Memory Leak Detect

Started by
1 comment, last by monkeyboi 11 years, 10 months ago
Hi I am trying to make a memory leak detection of my program, my reference information is in this link: http://forums.codeguru.com/showthread.php?t=312742

what I am doing is create a header file including:

#include "stdafx.h"
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif


I inteneded to put it in a header file like common.h so every other header files including this one could also have memory leak detection. However one problem is the compiler will give me errors about redefination of new operator, I will plaste the errors information in the end of this post due to it is quite long. I can solve the problem by putting #include "LumPyDetectMemoryLeak.h" in the cpp files instead of header files, which seems really awkward inefficient. Any one have any idea how to solve this problem in another way? Plus even though I include LumPyDetectMemoryLeak.h in all of my cpp files, In the end there are still some outputs that do not show where the memory leaks are. So I am wondering if I need to define the same thing in inline files or some other places?


c:\users\jerry\desktop\lumpy\lumpy\test.cpp(23) : {136} client block at 0x002C7848, subtype 0, 192 bytes long.
Data: < , > 94 09 EB 00 00 00 00 00 18 18 2C 00 CD CD CD CD
c:\users\jerry\desktop\lumpy\lumpy\lumpystring.cpp(17) : {135} client block at 0x002C17D8, subtype 0, 1 bytes long.
Data: < > 00
c:\users\jerry\desktop\lumpy\lumpy\test.cpp(21) : {134} client block at 0x002C7508, subtype 0, 768 bytes long.
Data: <8 , > 38 FE EA 00 00 00 00 00 D8 17 2C 00 CD CD CD CD
{125} normal block at 0x002C1778, 32 bytes long.
Data: < > A4 18 EB 00 CD CD CD CD CD CD CD CD CD CD CD CD
{124} normal block at 0x002C1718, 32 bytes long.





j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : error C2365: 'operator new' : redefinition; previous definition was 'function'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : error C2491: 'new' : definition of dllimport data not allowed
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : error C2078: too many initializers
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : error C2143: syntax error : missing ';' before '('
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : error C2226: syntax error : unexpected type 'size_t'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(33) : error C2059: syntax error : ')'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : warning C4229: anachronism used : modifiers on data are ignored
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C2365: 'operator new' : redefinition; previous definition was 'function'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C2491: 'new' : definition of dllimport data not allowed
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C2078: too many initializers
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C2143: syntax error : missing ';' before '['
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C3409: empty attribute block is not allowed
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(36) : error C2226: syntax error : unexpected type 'size_t'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(143) : error C2059: syntax error : 'constant'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(144) : error C2091: function returns function
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(144) : error C2802: static member 'operator new' has no formal parameters
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(144) : error C2333: 'std::locale::facet::operator new' : error in function declaration; skipping function body
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(149) : error C2059: syntax error : 'constant'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(151) : error C2091: function returns function
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(151) : error C2556: 'void *(__cdecl *std::locale::facet::operator new(void))(size_t,const std::_DebugHeapTag_t &,char *,int)' : overloaded function differs only by return type from 'void *(__cdecl *std::locale::facet::operator new(void))(size_t)'
1> j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(143) : see declaration of 'std::locale::facet::operator new'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(152) : error C2059: syntax error : 'return'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(152) : error C2238: unexpected token(s) preceding ';'
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(155) : error C2628: 'std::locale::facet' followed by 'void' is illegal (did you forget a ';'?)
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(157) : error C2487: 'delete' : member of dll interface class may not be declared with dll interface
1>j:\program files (x86)\microsoft visual studio 9.0\vc\include\xlocale(157) : error C2817: return type for 'operator delete' must be 'void'
Advertisement
There is an option in Visual Studio "Force Include Files". I have never used it. Seems to be the solution found here.

Are you sure you actually need to use _CLIENT_BLOCK?

_NORMAL_BLOCK
If you intend to use Normal blocks only, and have no need for Client blocks, you may want to define _CRTDBG_MAP_ALLOC, which causes all heap allocation calls to be mapped to their debug equivalents in Debug builds. This will allow file name and line number information about each allocation call to be stored in the corresponding block header.

...

_CLIENT_BLOCK

An application can keep special track of a given group of allocations for debugging purposes by allocating them as this type of memory block, using explicit calls to the debug heap functions. MFC, for example, allocates all CObjects as Client blocks; other applications might keep different memory objects in Client blocks.
[/quote]
Hi rip-off

Thank you so much. Your reply really help me a lot!
I think I need to do more research about the block types tho.

This topic is closed to new replies.

Advertisement