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

Source: Internet
Author: User

Link: http://www.codeguru.com/forum/archive/index.php/t-269905.html

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 deployments 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 deployments: "Win32 debug" and "Win32 release ". these are just convenient starter deployments 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 invocations, 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;
Assert (DLG. Create (idd_some_dlg ));
DLG. showwindow (sw_show );

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, shocould 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 shoshould 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 serving ving the release version (http://www.codeproject.com/debug/survivereleasever.asp ).

 

Another excellent article: Http://www.codeproject.com/KB/debug/survivereleasever.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.