code within statically linked library src files returning null

asked 2022-06-16 03:25:28 -0600

AEHadi gravatar image

updated 2022-06-16 14:08:52 -0600

This is for windows: I have been stuck on this issue for two days now and I still am at a loss as to what is going on. Any help will be greatly appreciated.

I have a library that I am statically linking against. Library A. it is built with colcon. Could this be a linking issue or an issue with the fact that I build library A with a certain set of preprocessors and I build library B with a different set of preprocessors that change the Gameobject class to a different version shown below but same function implementations.

ament_auto_add_library(A STATIC 
        ${SOURCES}
        ${HEADERS}
)
ament_target_dependencies(A ${ALL_DEPENDS})
install(TARGETS
  A
  EXPORT A_export  
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)
ament_export_libraries(A_export HAS_LIBRARY_TARGET)

I then link to it from a ros2 package using the standard target_link_libraries(B A)

I have a object called Gameobject that is defined in library A.

#file --------------------------Gameobject.h

    #ifdef InLibraryA

    class Gameobject
    {
         int GetPosx(){return data.x;}
    }

    #else
    class Gameobject
    {
        int GetPosx(){return data.x;}
    }
    #endif

#file --------------------------MoveGameObject.h
class MoveGameObject
{
      int MoveGObj_inline(Gameobject* g)
     {
       return  g->getPosx();
     } 

     int MoveGObj(Gameobject* g);
}

#file --------------------------MoveGameObject.cpp
 int MoveGameObject::MoveGObj(Gameobject* g)
{
     return  g->getPosx();
}

Now in library B , I do this within a subscription callback

SomeCallback()
{
      Gameobject* g = GetGObjectFromPool();
     MoveGameObject* m= new MoveGameObject();

     //this will return NULL value
     int posx = m->MoveGObj(g);

     //this will be fine because it was inlined?
     int possx = m->MoveGObj_inline(g);

}

You will see that I get null when calling the function that was NOT inlined for calling the getter function from Gameobject. I dont get null for the inlined function even though they run the exact same code. Note that this only happens to non-inline functions that call Gameobject functions. Does not happen to functions that do not read memory from Gameobject. addingTwoInts() for example works fine Non-inline. There are no errors. It is undefined behavior. Any ideas on what I could be doing wrong for this to happen? The simplified code above is the same as to what is happening in my code, just removed unnecessary details.

edit retag flag offensive close merge delete