Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-12-08
4
Medium Priority
?
810 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 500 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses

886 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