Solved

win32 console app memory leak problem

Posted on 1997-06-26
3
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

728 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