Best practices and tools for debugging differences between Debug and Release builds?

后端 未结 6 1777
失恋的感觉
失恋的感觉 2021-02-14 00:40

I\'ve seen posts talk about what might cause differences between Debug and Release builds, but I don\'t think anybody has addressed from a development standpoint what is

相关标签:
6条回答
  • 2021-02-14 01:08

    The most obvious cause is simply the use of #ifdef and #ifndef directives associated DEBUG or similar symbol that change between the two builds.

    Before going down the debugging road (which is not my personal idea of fun), I would inspect both command lines and check which flags are passed in one mode and not the other, then grep my code for this flags and check their uses.

    One particular issue that comes to mind are macros:

    #ifdef _DEBUG_
      #define CHECK(CheckSymbol) { if (!(CheckSymbol)) throw CheckException(); }
    #else
      #define CHECK(CheckSymbol)
    #endif
    

    also known as a soft-assert.

    Well, if you call it with a function that has side effect, or rely on it to guard a function (contract enforcement) and somehow catches the exception it throws in debug and ignore it... you will see differences in release :)

    0 讨论(0)
  • 2021-02-14 01:17

    When debug and release differ it means:

    1. you code depends on the _DEBUG or similar macros (defined when compiling a debug version - no optimizations)
    2. your compiler has an optimization bug (I seen this few times)

    You can easily deal with (1) (code modification) but with (2) you will have to isolate the compiler bug. After isolating the bug you do a little "code rewriting" to get the compiler generate correct binary code (I did this a few times - the most difficult part is to isolate the bug).

    I can say that when enabling debug information for release version the debugging process works ... (though because of optimizations you might see some "strange" jumps when running).

    You will need to have some "black-box" tests for your application - valgrind is a solution in this case. These solutions help you find differences between release and debug (which is very important).

    0 讨论(0)
  • 2021-02-14 01:20

    When I come across a bug that only happens in release, the first thing I always look for is use of an uninitialized stack variable in the code that I am working on. On Windows, the debug C runtime will automatically initialise stack variables to a know bit pattern, 0xcdcdcdcd or something. In release, stack variables will contain the value that was last stored at that memory location, which is going to be an unexpected value.

    Secondly, I will try to identify what is different between debug and release builds. I look at the compiler optimization settings that the compiler is passed in Debug and Release configurations. You can see this is the last property page of the compiler settings in Visual Studio. I will start with the release config, and change the command line arguments passed to the compiler one item at a time until they match the command line that is used for compiling in debug. After each change I run the program and reproducing the bug. This will often lead me to the particular setting that causes the bug to happen.

    A third technique can be to take a function that is misbehaving and disable optimizations around it using the pre-processor. This will allow you run the program in release with the particular function compiled in debug. The behaviour of the program which has been built in this way will help you learn more about the bug.

    #pragma optimize( "", off )
    void foo() {
        return 1;
    }
    #pragma optimize( "", on ) 
    

    From experience, the problems are usually stack initialization, memory scrubbing in the memory allocator, or strange #define directives causing the code to be compiled incorrectly.

    0 讨论(0)
  • 2021-02-14 01:20

    The best solution is to set up something like automated unit testing to thoroughly test all aspects of the application (not just individual components, but real world tests which use the application the same way a regular user would with all of the dependencies). This allows you to know immediately when a release-only bug has been introduced which should give you a good idea of where the problem is.

    Good practice to actively monitor and seek out problems beats any tool to help you fix them long after they happen.

    However, when you have one of those cases where it's too late: too many builds have gone by, can't reproduce consistently, etc. then I don't know of any one tool for the job. Sometimes fiddling with your release settings can give a bit of insight as to why the bug is occurring: if you can eliminate optimizations which suddenly make the bug go away, that could give you some useful information about it.

    Release-only bugs can fall into various categories, but the most common ones (aside from something like a misuse of assertions) is:

    1) Uninitialized memory. I use this term over uninitialized variables as a variable may be initialized but still be pointing to memory which hasn't been initialized properly. For this, memory diagnostic tools like Valgrind can help.

    2) Timing (ex: race conditions). These can be a nightmare to debug, but there are some multithreading profilers and diagnostic tools which can help. I can't suggest any off the bat, but there's Coverity Integrity Manager as one example.

    0 讨论(0)
  • 2021-02-14 01:24

    The very existence of two configurations is a problem from debugging point of view. Proper engineering would be such that the system on the ground and in the air behave the same way, and achieve this by reducing the number of ways by which the system can tell the difference.

    Debug and Release builds differ in 3 aspects:

    • _DEBUG define
    • optimizations
    • different version of the standard library

    The best way around, the way I often work, is this:

    • Disable optimizations where performance is not critical. Debugging is more important. Most important is disable function auto-inlining, keep standard stack frame and variable reuse optimizations. These annoy debug the most.
    • Monitor code for dependence on DEBUG define. Never use debug-only asserts, or any other tools sensitive to DEBUG define.
    • By default, compile and work /release.
    0 讨论(0)
  • 2021-02-14 01:27

    One other "Best Practice", or rather a combination of two: Have Automated Unit Tests, and Divide and Conquer.

    If you have a modular application, and each module has good unit tests, then you may be able to quickly isolate the errant piece.

    0 讨论(0)
提交回复
热议问题