Solved

win32 console app memory leak problem

Posted on 1997-06-26
3
383 Views
Last Modified: 2013-12-14
Some time ago I went to a c++ class and one of the things they covered was finding memory leaks.  For the life of me, I cannot remember how to do it.  What is the best method of finding memory leaks.  I think I deallocated everthing, but I can't be sure.  I'm having some debug assertion failure problems and I believe it's because of some leaks.
0
Comment
Question by:ivanh
3 Comments
 

Expert Comment

by:Knallar
ID: 1164150
1. Check all new instruction and use delete rsp. delete[], if you    used  new[]

2. Use Tools like: Purify or Boundchecker
0
 
LVL 1

Author Comment

by:ivanh
ID: 1164151
Sorry, that's not the answer I was looking for.  I want some
code that's inline with my app that will tell me (maybe apon
termination or whatever) as to when or if I have a possible
memory leak.  I have tried my best to delete when I have a new, but I have apparently missed some and having a hard time finding the culprit.
0
 
LVL 2

Accepted Solution

by:
anichini earned 100 total points
ID: 1164152
Here ya go: (although VC++ 5.0 has some memory leak detection built into the runtime library, but I've never got it to record line numbers correctly)

Include leaks.h in your source files, and compile leaks.cpp. In general, I'd include it after any system includes. By defining DETECT_LEAKS, you can tell it to record information about where bits of memory were allocated. Use the CHECK_LEAKS() function to print out currently allocated memory.

// *************************** leaks.h **************************
#ifndef __LEAKS_H__
#define __LEAKS_H__

#ifdef DETECT_LEAKS
struct MemHeader
{
      MemHeader *pNext;
      const char *pscFile;
      int nLine;
};

void *operator new(size_t size, const char *pscFile, int nLine);
// this insures that if any code calls just plain new, it'll still set up the mem header
inline void *operator new(size_t size)
{
      return ::operator new(size, "Normal", -1);
}

void operator delete(void *p);
void checkLeaks();

#ifndef DETECT_LEAKS_INTERNAL
#ifdef new
#undef new
#endif
#define new new(__FILE__, __LINE__)
#define CHECK_LEAKS() checkLeaks()
#endif // DETECT_LEAKS_INTERNAL

#else // DETECT_LEAKS

#define CHECK_LEAKS()

#endif // DETECT_LEAKS
#endif // __LEAKS_H__

// ************************ leaks.cpp **************************
#include <iostream>
using namespace std;

#define DETECT_LEAKS
#define DETECT_LEAKS_INTERNAL
#include "leaks.h"
#include <assert.h>

MemHeader g_allocList = { NULL, "Dummy", 0};

void *operator new(size_t size, const char *pscFile, int nLine)
{
      MemHeader *pMem;

      pMem = (MemHeader *)malloc(size+sizeof(MemHeader));
      pMem->pNext = g_allocList.pNext;
      pMem->pscFile = pscFile; // ok not to copy, __FILE__ creates a const string in the code segment
      pMem->nLine = nLine;
      g_allocList.pNext = pMem;

      return (void *)(((char *)pMem)+sizeof(MemHeader));
}

void operator delete(void *p)
{
      MemHeader *pMem;
      MemHeader *pStart = &g_allocList;

      pMem = (MemHeader *)(((char *)p)-sizeof(MemHeader));
      // this will be kinda slow, you could solve this by making the linked list doubly linked
      while(pStart != NULL)
      {
            if(pStart->pNext == pMem)
                  break;
            pStart = pStart->pNext;
      }
      assert(pStart != NULL);
      pStart->pNext = pMem->pNext;
      free(pMem);
}

void checkLeaks()
{
      MemHeader *pStart = g_allocList.pNext;

      while(pStart != NULL)
      {
            if(pStart->nLine != -1)
            {
                  cerr << "Memory leak detected! file: " << pStart->pscFile << " line: " << pStart->nLine << endl;
            }
            pStart = pStart->pNext;
      }
}

// ************************ test.cpp ***************************
#include <iostream>
using namespace std;
#define DETECT_LEAKS      
#include "leaks.h"

void main()
{
      int *p1, *p2;

      p1 = new int[32];
      p2 = new int[64];

      CHECK_LEAKS();
      cerr << endl;
      delete p1;
      CHECK_LEAKS();
      cerr << endl;
      delete p2;
      CHECK_LEAKS();
      cerr << endl;
}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now