Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

win32 console app memory leak problem

Posted on 1997-06-26
3
Medium Priority
?
417 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

618 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