CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2002
    14° 39'19.65"N / 121° 1'44.34"E

    Visual C++ Debugging: Why does program work in debug mode, but fail in release mode?

    Q: Why does program work in debug mode, but fail in release mode?

    A: First of all, there is no such thing as 'debug mode' or 'release mode'. The VC++ IDE offers the possibility to define configurations which include a set of project settings (like compiler / linker options, output directories etc.) When a project is created using AppWizard, you get two default configurations: "Win32 Debug" and "Win32 Release". These are just convenient starter configurations with several preset options which are suitable for typical debug builds or release builds respectively, but you are by no means restricted to those settings. Actually, you can modify those configurations, delete them, or create new ones. Now let's see what the two default configurations typically include and what distinguishes them:

    Win32 Debug:

    • Subdirectory 'Debug' used for temporary and output files
    • Preprocessor symbol _DEBUG defined
    • Debug version of the runtime libraries is used
    • All compiler optimizations turned off
    • Generate debug info

    Win32 Release:
    • Subdirectory 'Release' used for temporary and output files
    • Preprocessor symbol NDEBUG defined
    • Release version of the runtime libraries is used
    • Various compiler optimizations turned on
    • Generate no debug info

    There are a few other differences, but these are the most important ones. Now, what's the first implication of all this? That, as opposed to a common misunderstanding, you can debug a release build. Just go to 'Project -> Settings', choose the Win32 Release configuration, tab 'C/C++', 'General' and set 'Debug Info' to 'Program Database'. Then go to the tab 'Linker', and turn on 'Generate Debug Info'. If you rebuild your project now, you will be able to run it in the debugger. Regardless of whether your program crashes or just doesn't behave as expected, running it in the debugger will show you why. Note however, that due to optimizations turned on in the release build, the instruction pointer will sometimes be off by a few code lines, or even skip lines altogether (as the optimizer didn't generate code for them). This shouldn't be a concern, if it is, turn off optimizations.

    When debugging your release build this way, you will probably discover that at a certain point during execution, a variable has a different value in the release and in the debug build, causing the differing behaviour. And if you go back and see where the value of that variable is set, you will most probably find out that it isn't: You simply forgot to initialize that variable. The reason why the debug build seemed to work is that the debug version of the runtime library initializes dynamic memory and stack variables to known values (in order to track down memory allocation and overwrite errors), while the release version of the runtime library doesn't. This is by far the most frequent single cause for different behaviour between debug and release builds, so chances are good that this fixes your problem (and for the future, remember to always initialize your variables).

    If uninitialized variables were not the cause of your problem, let's look at the next possible difference between debug and release builds: The preprocessor symbols _DEBUG and NDEBUG. If you have any code inside an #ifdef _DEBUG / #endif block, it will not be contained in a release build. What's worse, the dependency of those symbols can be hidden inside other macros. A typical candidate for this is ASSERT: It expands to the assertion testing code if _DEBUG is defined, and to nothing if it is not. Therefore, be careful to have no code with side effects inside an ASSERT macro. For example, the following code will work in a debug build, but fail in a release build:

    CSomeDialog dlg;
    As a rule, never put code which needs to be executed inside an ASSERT. (A side note: Conditions which can be expected to fail at runtime, like the 'Create()' call in the example, should never be tested with ASSERTs anyway. Assertions are a tool to assert pre- and postconditions regarding your code, not runtime error conditions.)

    At this point, you have most probably found out why your code failed in the release build. If not, this might be one of the very rare cases where the compiler optimizations caused your code to behave differently (the VC++ compiler had several optimizer bugs in the past, and I doubt they have all been fixed). To exclude this, first turn all the optimizations off (Project -> Settings, tab 'C/C++', category 'Optimizations', option 'Disable (Debug)'). If your code works now, selectively turn optimization options on until you found the culprit. Simply leave it turned off, or upgrade to a newer version of the compiler (or install the most recent service packs) which might hopefully fix that bug.

    This should help you get your release build running in most of the situations. For a more in-depth discussion about the differences between debug and release builds, see the excellent article Surviving the Release Version.

    Last edited by Andreas Masur; July 25th, 2005 at 01:03 AM.

  2. #2
    Join Date
    May 2002
    Phoenix, AZ
    Incorrectly prototyped message handlers !

    Thats another big goof up everyone makes atleast once ...
    In the debug build, if you have incorrect message handler signatures this doesnt cause any problems.

    But MFC does a couple of naughty type casts in the message map macros. So when you build the same code in release mode, you are guranteed to run into trouble. As soon as your message handler returns, your stack frame gets trashed and you get a neat little GPF.

    Last edited by Andreas Masur; July 25th, 2005 at 01:03 AM.

  3. #3
    Join Date
    Mar 2001

    Re: Visual C++ Debugging: Why does program work in debug mode, but fail in release mo

    As an addition to debugging the release mode:

    All sub projects should need still turn on debug info. (depending on your configuration you will have to do this).

    Configuration Properties->C++->General->Debug Information format

    Should set to Zi or ZI. Program Database or program database for edit and continue. Just be consistant through your project.
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width