Solved

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

Posted on 2003-12-08
4
790 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

732 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