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

后端 未结 18 1540
余生分开走
余生分开走 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: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.

提交回复
热议问题