Common reasons for bugs in release version not present in debug mode

后端 未结 18 1451
余生分开走
余生分开走 2020-11-28 04:11

What are the typical reasons for bugs and abnormal program behavior that manifest themselves only in release compilation mode but which do not occur when in debug mode?

相关标签:
18条回答
  • 2020-11-28 04:43

    Many times, in debug mode in C++ all variables are null initialized, whereas the same does not happen in release mode unless explicitly stated.

    Check for any debug macros and uninitialized variables

    Does your program uses threading, then optimization can also cause some issues in release mode.

    Also check for all exceptions, for example not directly related to release mode but sometime we just ignore some critical exceptions, like mem access violation in VC++, but the same can be a issue at least in other OS like Linux, Solaris. Ideally your program should not catch such critical exceptions like accessing a NULL pointer.

    0 讨论(0)
  • 2020-11-28 04:44

    It can, especially if you are in the C realm.

    One cause could be that the DEBUG version may add code to check for stray pointers and somehow protect your code from crashing (or behave incorrectly). If this is the case you should carefully check warnings and other messages you get from your compiler.

    Another cause could be optimization (which is normally on for release versions and off for debug). The code and data layout may have been optimized and while your debugging program just was, for example, accessing unused memory, the release version is now trying to access reserved memory or even pointing to code!

    EDIT: I see other mentioned it: of course you might have entire code sections that are conditionally excluded if not compiling in DEBUG mode. If that's the case, I hope that is really debugging code and not something vital for the correctness of the program itself!

    0 讨论(0)
  • 2020-11-28 04:47

    In .NET, even if you don't use conditional compilation like #if DEBUG, the compiler is still alot more liberal with optimisations in release mode than it is in debug mode, which can lead to release only bugs as well.

    0 讨论(0)
  • 2020-11-28 04:52

    Yes!, if you have conditional compilation, there may be timing bugs (optimised release code verse, non-optimised debug code), memory re-use vs. debug heap.

    0 讨论(0)
  • 2020-11-28 04:52

    In a non-void function, all execution paths should end with a return statement.

    In debug mode, if you forget to end such a path with a return statement then the function usually returns 0 by default.

    However, in release mode your function may return garbage values, which may affect how your program runs.

    0 讨论(0)
  • 2020-11-28 04:55

    I've been bitten by a number of bugs in the past that have been fine in Debug builds but crash in Release builds. There are many underlying causes (including of course those that have already been summarised in this thread) and I've been caught out by all of the following:

    • Member variables or member functions in an #ifdef _DEBUG, so that a class is a different size in a debug build. Sometimes #ifndef NDEBUG is used in a release build
    • Similarly, there's a different #ifdef which happens to be only present in one of the two builds
    • The debug version uses debug versions of the system libraries, especially the heap and memory allocation functions
    • Inlined functions in a release build
    • Order of inclusion of header files. This shouldn't cause problems, but if you have something like a #pragma pack that hasn't been reset then this can lead to nasty problems. Similar problems can also occur using precompiled headers and forced includes
    • Caches: you may have code such as caches that only gets used in release builds, or cache size limits that are different
    • Project configurations: the debug and release configurations may have different build settings (this is likely to happen when using an IDE)
    • Race conditions, timing issues and miscellanous side-effects occurring as a result of debug only code

    Some tips that I've accumulated over the years for getting to the bottom of debug/release bugs:

    • Try to reproduce anomalous behaviour in a debug build if you can, and even better, write a unit test to capture it
    • Think about what differs between the two: compiler settings, caches, debug-only code. Try to minimise those differences temporarily
    • Create a release build with optimisations switched off (so you're more likely to get useful data in the debugger), or an optimised debug build. By minimising the changes between debug and release, you're more likely to be able to isolate which difference is causing the bug.
    0 讨论(0)
提交回复
热议问题