Solved

win32 console app memory leak problem

Posted on 1997-06-26
3
389 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

831 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