Solved

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

Posted on 2003-12-08
4
774 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now