views:

291

answers:

8

Is there any reason for a program to leak when compiled in Debug mode and not in release?

(Debug means debug informations, and compiler optimization disabled, Release means no debug info / full optimization)

That's what it seems to do but I can't figure out why. Btw purify is not being helpful here

A: 

conditional #ifdef _DEBUG lines could be one reason.

Remus Rusanu
+4  A: 

A lot of pointer type errors, including memory leaks, can seem to appear or disappear when switching between debug and release mode. A couple of reasons might be:

  • Conditional code compiled in one version or the other
  • Memory locations of things move around
  • Special formatting of uninitialized data in the debug version
Trent
+1 for the mention of uninitialized data. Just fixed some bugs not too long ago that were caused by that.
DShook
+3  A: 

How are you detecting the leak? If it's via the task manager, the MSVC debug implementation will keep freed memory around when the _CRTDBG_DELAY_FREE_MEM_DF flag is set.

It's also possible that you have a memory leak that does not exist in Release.

EDIT: You can also manually call HeapCompact(GetProcessHeap(), 0). I seem to recall that the debug heap always grows (i.e., it doesn't return free blocks), but I can't find that documentation anywhere.

MSN
As stated in the question, I use purify. (see http://www-01.ibm.com/software/awdtools/purify )
f4
How do you know it's leaking if purify isn't helping?
MSN
see my comment on the question
f4
+3  A: 

Here is another one, assert() calls with sideeffects, this might result in bigger problems

assert (new Object());

might cause this behaviour, if assert gets optimized out in release mode

Harald Scheirich
A: 

Give a try to User Mode Dump Heap which comes with Debugging Tools for Windows, also take a look at what Application Verifier might have to say. All of those tools are extremely powerful, highly recommend.

Otherwise, unless you have leaking code, which is #ifdef-ed, I see no reason for Debug build to leak, while Release is fine.

Dmitry
+1  A: 

Debug and Release mode use a different memory model.

There are cases where are program runs in one mode and crashes in the other one.

Something that may cause this is memory corruption (especially stack corruption). That may be a reason why there are differences. Another reason could be that the debugger doesnt free everything, but i doubt it.

Btw are you using VS 2010 beta? That may also be a bug in the beta version.

George B.
+1  A: 

The first thing you want to try to isolate is whether or not the leak is due to #define _DEBUG or something more subtle and potentially harder to pinpoint - i.e. something getting optimized that shouldn't have been.

Compile Release with #define _DEBUG and see if it still happens.

Igor Zevaka
A: 

It could be many things, but keep in mind it may be a false positive. In debug mode you can count on using at least twice the memory. This can make your program size expand many times, and usually once it goes up it doesn't go down again even if the memory all gets released internally. If the 'leak' doesn't get worse over time but eventually stops it's not really a leak.

Charles Eli Cheese