Reputation: 730
In older Version of Visual C++, the debugger was able to detect memory leaks. For example the following code
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char *memleak= new char[100];
memleak[0]='a';
return 0;
}
should result in a message that there is memleak of 100bytes. Something like this: (See MSDN)
Detected memory leaks! Dumping objects -> {18} normal block at 0x00780E80, 100 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete.
But i'm not able to "force" this message. Is there something I have to enable? or do I have to install some extra features? I'm using Studio Prof. 2012 Update 4.
Upvotes: 0
Views: 795
Reputation: 11954
The answer by @Vinzenz is somewhat on spot but I'll try to give all the details. You basically have two options - either make the debug runtime dump the leaks when program exits (this can be done by turning on the memory leak reporting by calling _CrtSetDbgFlag with a flag value that has the _CRTDBG_LEAK_CHECK_DF
bit set), or as mentioned call _CrtDumpMemoryLeaks()
to dump the leaks at a random execution point. Since your example does not do any one of these things, you get nothing.
What is important about _CrtDumpMemoryLeaks()
that it will dump the heap allocations that have not freed at the point when it is called, so any smart pointers (and all other objects that allocate heap memory inside) that have not been destroyed will be dumped at this point. That is why using the report flag is somewhat better since it runs after the end of the execution of the program so all the objects that should be destroyed are destroyed.
As for the DBG_NEW
, it only gives you additional line information showing the line that caused the leak. Without it you will get output just like the example in the question, with it you'll get the number of the line that caused this (see example below).
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
/*
Without the DBG_NEW you get something like, no line info
Detected memory leaks!
Dumping objects ->
{74} normal block at 0x00000000005D6520, 100 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete
With it you get
Detected memory leaks!
Dumping objects ->
strcattest.cpp(36) : {74} normal block at 0x00000000002C6520, 100 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
*/
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
int main(int argc, char* argv[])
{
// Enable automatic memory leak reporting at the end of the program, the next 3 lines can be skipped if _CrtDumpMemoryLeaks() is called somewhere
int current_flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG );
current_flags |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(current_flags);
char *memleak= new char[100];
_CrtDumpMemoryLeaks(); // Trigger dumping the leaks at this point
return 0;
}
Upvotes: 2
Reputation: 3208
Did you read through that MSDN article? As you use new
for memory allocation, you have to add this line:
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
You can also call _CrtDumpMemoryLeaks()
to "force" an output of all detected memory leaks at any point of your program. I prefer to do this on exit points of my apps though.
Upvotes: 1