简体   繁体   中英

C++ program crashes when code is linked to exe but works fine when code is compiled into exe, how come?

I have a program that have been running fine for long time on one platform. Because of its success it is to be ported to another platform. No problem, I thought since it is written in Standard C++...

My approach (illustrated with pseudo CMake):

  • setup the development environment by sourcing the platform specific toolchain to ensure that correct platform is targeted
  • factor out all core business logic into an application object and build a library out of that (one library for each platform from the same source code):

     add_library(appLib STATIC app.cpp) target_link_libraries(appLib utilLib networkLib dbLib ${boostLibs}) 
  • have one main_a.cpp and another main_b.cpp, which do the platform-specific initialization for platform a and b respectively, and let the main function in those instantiate the application object.

     int main() { auto result = initAndDoPlatformStuff(); App app(result); app.run(); } 
  • instruct compiler and linker to assemble an executable:

     if (Platform_A) add_executable(appExe main_a.cpp) else() add_executable(appExe main_b.cpp) endif() target_link_libraries(appExe appLib) 

In principle, this is a perfectly valid approach I guess. But in reality it does not work. Within a second program crashes, and the crashes are different almost every time; inspecting the core dumps indicate it sometime crashed in the standard library, sometime in boost library and also in my code, but this is nonsense I guess. Program seem to work 1 out 10 times, but eventually crashes.

However, if I use the same exact code, only extract it into its original main.cpp file and then build it together differently, like this:

int main()
{
    auto result = initAndDoStuff();
    processForever(result); // Business logic
}
add_executable(appExe main.cpp)
target_link_libraries(appExe utilLib networkLib dbLib ${boostLibs})

then it works!

I'm puzzled and confused. I'm suspecting it has to do something with code layout, I've therefore played around with different variants of PIC and PIE but have had no success with that. Are there any tools available that allows you to get a comprehensive overview of the binary code layout? I know about nm, od, objdump but they are low-level and I don't know what to look for... Maybe I'm on the wrong path anyway, maybe the problem is related to something completely different. Does anyone got any hunch of what can cause this behavior? How else can I approach this problem?

Actually, the fault was mine. Of course... I really tried to get all details correct when I refactored the code into a lib, but obviously I was not careful enough, and blind when searching for the problem.
The problem, which I finally found, was that I still kept one variable as a local variable after refactoring, which then went out of scope causing deallocated memory to be referenced, which resulted in all sorts of undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM