Click here to Skip to main content
13,769,643 members
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 24 Jan 2003

Trace Utility for preventing memory leaks

, 24 Jan 2003
Rate this:
Please Sign up or sign in to vote.
Beware of memory leaks with new and delete.


Writing our own trace utility is much important for complicated and complex applications. This is useful since C++ does not have the concept of GC (garbage collector) unlike C# or Java which automatically takes care of those issues. A good trace file can help a lot if you are trying to find hard-to-find memory leaks that often arise during the production of an application.

So, what we need is an easy utility that detects the memory leaks in place when the application is run. Also, that finds out where exactly the memory leak is and how many bytes of memory is not freed.

Using the code

Mainly, we want to rewrite the new function so that whenever new is called, it adds the trace, and of course, for delete, we have to remove the trace. Both the methods should be ssynchronous with each other; failing to call delete will trigger a memory leak.

#define New new(__FILE__, __LINE__)
      inline void * __cdecl operator new(size_t size, 
                 const char *fileName, int lineNumber)
          void *ptr = (void *)malloc(size);
          addTrace((DWORD)ptr, size, fileName, lineNumber);
//And for Delete this is supposed to be called.

      void __cdecl operator delete(void *p)
void Dump()
              // The iterator that walks us through.
          AllocatedList::iterator i;
          DWORD totalSize = 0;
          char buf[1024];


          for(i = allocatedList->begin(); i != allocatedList->end(); i++) {
              sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d NOTFREED\n",
                  (*i)->fileName, (*i)->lineNumber, (*i)->address, (*i)->size);
              totalSize += (*i)->size;
              sprintf(buf,"There are no MEMORY LEAKS\n");
              sprintf(buf, "Total UNFREED MEMORY: %d bytes\n", totalSize);

For checking the results, we need a helper method that walks us through the memory leaks. Finally, we write a method that shows exactly where the memory is leaking, what is the address, and how many bytes are leaked.

char *str = "This is a Testing Program";
int len = strlen(str);
char *ptr;
ptr = New char[len+1];
delete ptr;

Take a look at the main function. Instead of calling new char[len+1];, I am calling New. This will add the trace, and delete will remove the trace.

Hope this one is useful too.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...

Comments and Discussions

GeneralOn Trace utility for memory leaks Pin
Shivani270218-Jul-06 1:54
memberShivani270218-Jul-06 1:54 
Questionis it useful for constructor taking some param? Pin
10-May-05 20:56
suss10-May-05 20:56 
Generalum, copy and paste from another website Pin
Anonymous29-Jun-04 10:06
memberAnonymous29-Jun-04 10:06 
QuestionMultithread safe? Pin
John M. Drescher16-Feb-04 4:45
memberJohn M. Drescher16-Feb-04 4:45 
AnswerRe: Multithread safe? Pin
Anonymous12-Aug-05 4:14
memberAnonymous12-Aug-05 4:14 
GeneralRe: Multithread safe? Pin
John M. Drescher12-Aug-05 4:25
memberJohn M. Drescher12-Aug-05 4:25 
GeneralIt's good article, and there is something to know Pin
objects28-Jan-03 18:44
groupobjects28-Jan-03 18:44 
GeneralRe: It's good article, and there is something to know Pin
Anonymous29-Jan-03 9:43
memberAnonymous29-Jan-03 9:43 
GeneralRe: It's good article, and there is something to know Pin
smalbon14-Oct-04 5:50
membersmalbon14-Oct-04 5:50 
The point is that this code is no good for applications which use the "new" operator to create instances of C++ classes. In that case the object's constructor is invoked, and likewise the destructor for "delete".

It is, of course, standard practice to create/destroy objects using new/delete.
GeneralRe: It's good article, and there is something to know Pin
29-Jan-03 15:45
suss29-Jan-03 15:45 
QuestionOnly works on Win32? Pin
Mandalis26-Jan-03 0:10
memberMandalis26-Jan-03 0:10 
AnswerRe: Only works on Win32? Pin
Michael Dunn26-Jan-03 0:29
sitebuilderMichael Dunn26-Jan-03 0:29 
AnswerRe: Only works on Win32? Pin
26-Jan-03 6:12
suss26-Jan-03 6:12 
GeneralRe: Only works on Win32? Pin
Mandalis27-Jan-03 20:36
memberMandalis27-Jan-03 20:36 
GeneralModify your code Pin
A. Riazi25-Jan-03 18:57
professionalA. Riazi25-Jan-03 18:57 
GeneralRe: Modify your code Pin
A. Samar25-Jan-03 20:01
sussA. Samar25-Jan-03 20:01 
QuestionHow is this better? Pin
Marc Clifton25-Jan-03 13:59
protectorMarc Clifton25-Jan-03 13:59 
AnswerRe: How is this better? Pin
PJ Arends25-Jan-03 14:32
professionalPJ Arends25-Jan-03 14:32 
GeneralThis not an MFC article. Pin
25-Jan-03 15:47
suss25-Jan-03 15:47 
GeneralSo god damn... Pin
Rickard Andersson2025-Jan-03 12:57
memberRickard Andersson2025-Jan-03 12:57 
GeneralRe: So god damn... Pin
25-Jan-03 16:18
suss25-Jan-03 16:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181117.1 | Last Updated 25 Jan 2003
Article Copyright 2003 by Unknown Author
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid