Solved

Why do I get always 'crtdbg.h' as memleak file?

Posted on 2003-12-08
4
785 Views
Last Modified: 2007-12-19
For example:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

void main( )
{
  _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

  wxString *pString = new wxString(wxT("test"));
  pString = new wxString(wxT(""));
}

This will give a leak like:

Detected memory leaks!
Dumping objects ->
c:\program files\coding\vc98\include\crtdbg.h(552) : {98} normal block at 0x002F2A38, 4 bytes long.
 Data: <| Z > 7C C0 5A 00
{97} normal block at 0x002F29C0, 52 bytes long.
 Data: <            t e > 01 00 00 00 04 00 00 00 13 00 00 00 74 00 65 00
c:\program files\coding\vc98\include\crtdbg.h(552) : {96} normal block at 0x002F2978, 4 bytes long.
 Data: < )/ > CC 29 2F 00
Object dump complete.



But this is always crtdbg.h ... Is there any way I can put in the REAL filename the leak is in? This would be extremely handy ... Hence now I need to place breakpoints and such...
0
Comment
Question by:G00fy
4 Comments
 
LVL 13

Accepted Solution

by:
SteH earned 125 total points
ID: 9897634
Have you this code below the headers?

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Is your code a debug build?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9897762
>>But this is always crtdbg.h ... Is there any way I can put in the REAL filename the leak is in?

Add teh fillowing to your code:

#ifdef _DEBUG
#ifndef _DBG_NEW

#include <crtdbg.h>

inline void* __operator_new(size_t __n) {
      return ::operator new(__n,_NORMAL_BLOCK,__FILE__,__LINE__);
}
inline void* _cdecl operator new(size_t __n,const char* __fname,int __line) {
      return ::operator new(__n,_NORMAL_BLOCK,__fname,__line);
}
inline void _cdecl operator delete(void* __p,const char*,int) {
      ::operator delete(__p);
}

#define _DBG_NEW new(__FILE__,__LINE__)
#define new _DBG_NEW


#endif // _DBG_NEW
#else

#define __operator_new(__n) operator new(__n)

#endif
0
 
LVL 9

Expert Comment

by:_ys_
ID: 9897860
crtdbg.h creates and uses a lot of static variables. As these are created and destroyed outside the main function, memory leaks such as these will be reported.
0
 

Author Comment

by:G00fy
ID: 9898576
Perfect SteH, works like a charm man :)

Just included it into standard header file & works flawless ;)


And yes, it's debug build
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question