My code works in Debug mode, but not in Release mode

10,685

Solution 1

Debug modes often initialize heap data allocations. The program might be dependent on this behavior. Look for variables and buffers that are not getting initialized.

Solution 2

1) Double check any and all code that depends on preprocessor macros.
2) Use assert() for verify program state preconditions. These must not be expected to impact program flow (ie. removing the check would still allow the code to provide the same end result) because assert is a macro. Use regular run-time conditionals when an assert won't do.
3) Indeed, never leave a variable in an uninitialized state.

Solution 3

By far the most likely explanation is differing undefined behavior in the two modes caused by uninitialized memory. Lack of thread safety and problems with synchronization code can also exhibit this kind of behavior because of differing timing environments between debug and release, but if your program isn't multi-threaded then obviously this can't be it.

Solution 4

I had experienced this and in my case it was because of one of my array of struct which suppose to have only X index, but my looping which check this struct was over checking to X+1 index. Interesting is debugging mode was running fine though I was on Visual C++ 2005.

I spent a few hours by putting in printf into my coding line by line to catch the bug. Anyone has good way to debug this kind of error please let me know.

Share:
10,685
Nima
Author by

Nima

I am a Software Engineer

Updated on June 04, 2022

Comments

  • Nima
    Nima about 2 years

    I have a code in Visual Studio 2008 in C++ that works with files just by fopen and fclose. Everything works perfect in Debug mode. and I have tested with several datasets. But it doesn't work in release mode. It crashes all the time. I have turned off all the optimizations, also there is no dependency to anything(in the linker), and also I have set these:

    Optimization: Disabled(/Od) Keep Unreferenced Data. Do Not Remove Redundant Optimize for Windows98: NO

    I still keep wondering how it should not work under these circumstances. What else should I turn off to let it work as in debug mode?

    I think if it works in release mode but not in debug mode, it might be a coding fault but the other way looks weird. isn't it?

    I appreciate any help. --Nima

  • Nima
    Nima about 14 years
    I double checked, I don't see any mistakes like that. :)
  • Nima
    Nima about 14 years
    1. There isn't any. 2. I'm not using assert().
  • Geoff
    Geoff about 14 years
    1) :) 2) Starting with assert from the very beginning would have helped you catch this problem very early on. :)
  • Amardeep AC9MF
    Amardeep AC9MF about 14 years
    One more thing to check for is the possibility that the buffers you are using for fread() are large enough. Buffer overruns might be tolerated in one build configuration but not another simply due to how things get arranged in memory and the somewhat different heap usage in debug vs. release.
  • Nima
    Nima about 14 years
    Your answer looks promissing but I couldn't locate the exact location of the issue. Trying to debug in release mode doesn't help since it jumps some parts of the code and suddenly crashs. I tried scaffolding(putting couts...) and here it happens: Obj* f(...) { Obj* obj= new Obj(..); ... cout<<"1"<<endl; return obj; } void main() { Obj *obj = f(...); cout<<"2"<<endl; } it prints 1 but not 2!
  • Amardeep AC9MF
    Amardeep AC9MF about 14 years
    Okay, that does sound like a buffer overrun. It would be happening in the constructor of Obj. Check if there is a stack buffer in there. If it overruns the stack it will overwrite the return address of f() then the return obj; call would explode.