Sign in to follow this  

Memory Leak Detect

This topic is 2015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[CODE]
#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
[/CODE]

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'

Share this post


Link to post
Share on other sites
There is an option in Visual Studio "[url="http://msdn.microsoft.com/en-us/library/8c5ztk84.aspx"]Force Include Files[/url]". I have never used it. Seems to be the solution found [url="http://stackoverflow.com/questions/5053382/override-new-operator-in-c-while-crtdbg-h-is-causing-conflicts"]here[/url].

Are you sure you actually need to use [url="http://msdn.microsoft.com/en-us/library/xk1akxc4.aspx"]_CLIENT_BLOCK[/url]?
[quote]
[/b]_NORMAL_BLOCK[b]
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.

...

[b]_CLIENT_BLOCK[/b]

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]

Share this post


Link to post
Share on other sites

This topic is 2015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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