Sign in to follow this  
Fire Lancer

Memory leak problem

Recommended Posts

I put the following at the start of each cpp file (befor any other includes):
#ifdef _DEBUG
#	define _CRTDBG_MAP_ALLOC
#	include <stdlib.h>
#	include <crtdbg.h>
#endif

and this at the end of my main thread (which is always the last to exit)
#	ifdef _DEBUG
		_CrtDumpMemoryLeaks();
#	endif

However it doesn't tell me the file and line where leaked memory was allocated in the debug window. Only the allocation number, memory location and number of bytes, plus the content of the first few bytes...eg:
Detected memory leaks!
Dumping objects ->
{649} normal block at 0x003C6968, 4 bytes long.
 Data: < |< > 80 7C 3C 00 
...
{512} normal block at 0x003C8080, 84 bytes long.
 Data: <                > 00 00 00 00 CD CD CD CD CD CD CD CD CD CD CD CD 
What have I done to stop it proving the line, and file where the memory was allocated? I am using the standard new and delete operators for all my heap allocations.

Share this post


Link to post
Share on other sites
The short answer is that using crtdbg.h to track memory leaks will only bring you to the point where malloc() is called. Since you use new to allocate memory, it doesn't track the point where the new is called. Just use the allocation ID to break into your program.

Share this post


Link to post
Share on other sites
ok I made this, but I'm having problems with "#define new new(__FILE__, __LINE__)" creating tons of compile erroer in xdebug.h

#ifndef _FL_INC_MEMDBG
#define _FL_INC_MEMDBG

#include <stdlib.h>
#include <crtdbg.h>
#include <exception>
inline void*__cdecl operator new(unsigned size, const char *file, unsigned line)
{
void* p = _malloc_dbg(size, _NORMAL_BLOCK, file, line);
if(!p)throw std::bad_alloc();
return p;
};
inline void __cdecl operator delete(void *p)
{
_free_dbg(p, _NORMAL_BLOCK);
};
inline void*__cdecl operator new[](unsigned size, const char *file, unsigned line)
{
void* p = _malloc_dbg(size, _NORMAL_BLOCK, file, line);
if(!p)throw std::bad_alloc();
return p;
};
inline void __cdecl operator delete[](void *p)
{
_free_dbg(p, _NORMAL_BLOCK);
};
#define new new(__FILE__, __LINE__)
#endif



...\vc\include\xdebug(32) : warning C4229: anachronism used : modifiers on data are ignored
...\vc\include\xdebug(32) : error C2365: 'operator new' : redefinition; previous definition was 'function'
...\vc\include\xdebug(32) : error C2491: 'new' : definition of dllimport data not allowed
...\vc\include\xdebug(32) : error C2078: too many initializers
...\vc\include\xdebug(32) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
...\vc\include\xdebug(32) : error C2143: syntax error : missing ';' before '('
...\vc\include\xdebug(32) : error C2226: syntax error : unexpected type 'size_t'
...\vc\include\xdebug(33) : error C2059: syntax error : ')'
...\vc\include\xdebug(36) : warning C4229: anachronism used : modifiers on data are ignored
...\vc\include\xdebug(36) : error C2365: 'operator new' : redefinition; previous definition was 'function'
...\vc\include\xdebug(36) : error C2491: 'new' : definition of dllimport data not allowed
...\vc\include\xdebug(36) : error C2078: too many initializers
...
...then carriers on to xloocale...
234 error(s), 12 warning(s)
[/code]

Share this post


Link to post
Share on other sites
Your problem seems to be that you're voodoo programming: trying to do things without understanding what they really do or how they work. Your macro replaces every instance of the identifier new with new(__FILE__, __LINE__). However, this only makes sense if the occurrence of new looks like new T(args). If it doesn't, then bad things will happen. For example, this could happen in an existing placement new expression or a declaration of a new overload.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this