Click here to Skip to main content
15,886,860 members
Articles / Programming Languages / C++

Tracing Utility in C++

Rate me:
Please Sign up or sign in to vote.
3.29/5 (7 votes)
3 Feb 20031 min read 72.3K   702   16   11
This article explains inserts and deletion of queues

Introduction

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 bugs or hard to find memory leaks that often raise during the production of the application.

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

Using the Code

Mainly, we wanted 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 synchronous with each other. Failing to call delete will trigger memory leak.

C++
#define DEBUG_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);
    return(ptr);
  };

For delete, this is supposed to be called:

C++
void __cdecl operator delete(void *p)
{
  removeTrace((DWORD)p);
  free(p);
};

For checking the results, we need a helper method that walks us through the memory leaks:

C++
void Dump()
{
  AllocatedList::iterator i;
  DWORD totalSize = 0;
  char buf[1024];

  if(!allocatedList)
    return;

  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);
    printf("%s",buf);
    totalSize += (*i)->size;
  }

  sprintf(buf, "\n---------------------------------\n");
  printf("%s",buf);
  if(!totalSize) 
  {
    sprintf(buf,"There are no MEMORY LEAKS\n");
    printf("%s",buf);
  }
    else
  {
    sprintf(buf, 
      "Total UNFREED MEMORY: %d bytes\n", 
      totalSize);
    printf("%s",buf);
  }
};

I have used a List to iterate walk the elements.

C++
main()
{
  char *str = "This is a Testing Program";
  int len = strlen(str);
  char *ptr;
  ptr = DEBUG_NEW char[len+1];
  strcpy(ptr,str);
  delete ptr;
  Dump();
}

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

I still can't find out if there was a way to call new instead of DEBUG_NEW directly.

License

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.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHelp TVITEM Pin
AJAYP5-Feb-03 20:38
AJAYP5-Feb-03 20:38 
GeneralRe: Help TVITEM Pin
Nader Michael15-Feb-03 21:32
Nader Michael15-Feb-03 21:32 
I think u can set a pointer to the TVITEM u want in the lParam.
To access the item directly without any hassle of setting ID for each item then query over the tree to find the item u want.

I think this is the main strategy in using the lParam in general in Controls as well as CallBack functions.

Nader Nashaat
GeneralOpps.. There is already one i have written Pin
Samar Aarkotti5-Feb-03 9:36
Samar Aarkotti5-Feb-03 9:36 
GeneralBetter solution Pin
Zoltan Csizmadia4-Feb-03 10:51
Zoltan Csizmadia4-Feb-03 10:51 
GeneralRe: Better solution Pin
WREY6-Feb-03 0:21
WREY6-Feb-03 0:21 
QuestionA re-submit? Pin
Rickard Andersson204-Feb-03 5:25
Rickard Andersson204-Feb-03 5:25 

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.