Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

920 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

13 Experts available now in Live!

Get 1:1 Help Now