Solved

win32 console app memory leak problem

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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