Sign in to follow this  
Eber Kain

Tracking Memory Leaks with meNew and meDelete

Recommended Posts

Using the technique listed here http://www.flipcode.com/cgi-bin/fcarticles.cgi?show=63785 I have been able to keep tabs on the memory leaks in my program, but I still get some that just point me to the new operator its self. Im trying to figure out how to do this thing, but im not having much luck. Here is the dump I get from MSVC, i think this has to do with STL because I use alot of <list> <string> <map> structures in my code. The leaks I have here are not that bad, but I would like to get them all cleared up.
Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {783} normal block at 0x01B007B8, 12 bytes long.
 Data: <            > B8 07 B0 01 B8 07 B0 01 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {775} normal block at 0x01B00518, 12 bytes long.
 Data: <            > 18 05 B0 01 18 05 B0 01 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {767} normal block at 0x00BAC5D8, 12 bytes long.
 Data: <            > D8 C5 BA 00 D8 C5 BA 00 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {759} normal block at 0x00BABFE0, 12 bytes long.
 Data: <            > E0 BF BA 00 E0 BF BA 00 CD CD CD CD 
{461} normal block at 0x00BAAFD8, 512 bytes long.
 Data: <                > 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {449} normal block at 0x00BAADF0, 16 bytes long.
 Data: <  K             > A4 D4 4B 00 01 00 00 00 00 00 00 00 00 00 00 00 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {87} normal block at 0x00BA3770, 33 bytes long.
 Data: < C              > 00 43 00 CD CD CD CD CD CD CD CD CD CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {86} normal block at 0x00BA3708, 40 bytes long.
 Data: <  K             > F8 D3 4B 00 02 00 00 00 00 00 00 00 00 00 00 00 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {41} normal block at 0x00BA07B8, 1 bytes long.
 Data: < > CD 
Object dump complete.
Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {783} normal block at 0x01B007B8, 12 bytes long.
 Data: <            > B8 07 B0 01 B8 07 B0 01 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {775} normal block at 0x01B00518, 12 bytes long.
 Data: <            > 18 05 B0 01 18 05 B0 01 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {767} normal block at 0x00BAC5D8, 12 bytes long.
 Data: <            > D8 C5 BA 00 D8 C5 BA 00 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {759} normal block at 0x00BABFE0, 12 bytes long.
 Data: <            > E0 BF BA 00 E0 BF BA 00 CD CD CD CD 
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {41} normal block at 0x00BA07B8, 1 bytes long.
 Data: < > CD 
Object dump complete.
The thread 0xC48 has exited with code 0 (0x0).
The program 'C:\Programming\4x\Client.exe' has exited with code 0 (0x0).

Share this post


Link to post
Share on other sites
Seeing as you've posted no code it's kind of hard to point out a place where you might be leaking memory.

Share this post


Link to post
Share on other sites
Well, if I knew where I was leaking, then I could fix it, and I assume noone wants to wade through tens of thousands of lines of source code.

Most of the time, those dumps link to my source code, but those dont. They just direct to this.. which is crtdbg.h and not my code.

inline void* __cdecl operator new(unsigned int s)
{ return ::operator new(s, _NORMAL_BLOCK, __FILE__, __LINE__); }


I was wondering if anyone had any ideas what I should look for, or other ways I can try to figure out where the leaks are at? Or are they even leaks in my program?

Share this post


Link to post
Share on other sites
Try using crtbreakalloc, then you can pause execution at the desired allocation and step back to see who or what it is. At the entry point of your app;


_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(783);



In your original post, you can see the allocation number in curly braces( {783}, {775}, {767} etc ).

Share this post


Link to post
Share on other sites
Thats a handly little function, thanks. After some investigation I found that it is all from local instances of ifstream and string. Not much I can do about it. Some of them even appear to be inside or before the _CrtSetDbgFlag().

Here is a super short program, that still has leaks.


//=============================================================================
// Memory Leak Tracking Stuff
//=============================================================================
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#define meMalloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define meCalloc(c, s) _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define meRealloc(p, s) _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define meExpand(p, s) _expand_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define meFree(p) _free_dbg(p, _NORMAL_BLOCK)
#define meMemSize(p) _msize_dbg(p, _NORMAL_BLOCK)

#define meNew new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define meDelete delete

//=============================================================================
//Local Headers
//=============================================================================
#include <string>
#include <fstream>

using namespace std;

void main(void) {
//track memory leaks
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(41);

//program code
if(true) {
ifstream file;
string path = "testfile.txt";

file.open(path.c_str(), ifstream::in);
file.close();
}

//memory leak dump
_CrtDumpMemoryLeaks();

}


Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {42} normal block at 0x00320820, 33 bytes long.
Data: < C > 00 43 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {41} normal block at 0x003207B8, 40 bytes long.
Data: <4 F > 34 82 46 00 02 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
The thread 0x844 has exited with code 1 (0x1).
The program 'C:\Programming\4x\CodeTesting.exe' has exited with code 1 (0x1).

Share this post


Link to post
Share on other sites
By the 'vc98' in your path, should I assume you're using VC6? Get rid of that piece of trash. Now that visual c++ 2005 express is released and free, there is no excuse for people to be developing with that broken compiler and it's broken pre-standard libraries.

Also, main() returns int, not void. Have a nice day.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this