Is there an easy way to detect a memory leak?

Started by
8 comments, last by pinacolada 15 years, 4 months ago
I have a program I finished for class, and it works fine but I suspect there is a memory leak in it. It's not something I have to worry about since the project is focusing more on a concept but I would still like to fix it for my own personal gain. The reason I suspect there is a memory leak is because I am working with linked lists, using the "new" operator, but have no corresponding delete (I'm really not sure where to put one.) My question is there anything in Visual Studio 2008 that lets you detect a memory leak? I was going to post my code but I didn't want the thread to get shut down if someone thought I was after homework help.
Advertisement
The easiest way to do basic leak checking in MSVC is to use the CRT debug functions. In this case you'd probably want to use _CrtSetDbgFlag() to create a leak check on program exit. Ex:
#include <crtdbg.h>int main(int, char **) {  _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );  // do stuff}
Here's the way I use to delete a linked list. I haven't done this in a while because I use STL or another container class for the most part.

class LL{   //could have previous as well   LL * next;   ...   ~LL()   {      if( next != NULL )         delete next;   }};
Quote:Original post by Chrono1081
The reason I suspect there is a memory leak is because I am working with linked lists, using the "new" operator, but have no corresponding delete (I'm really not sure where to put one.)
If you call new, and don't call a matching delete later on, then by definition you have a memory leak.
Quote:I have a program I finished for class, and it works fine but I suspect there is a memory leak in it. It's not something I have to worry about since the project is focusing more on a concept but I would still like to fix it for my own personal gain.
Implementing linked data structures for class tends to be a nightmare in this respect, as the majority of professors still expect you to code as if in C (all free functions, minimal cleanup code, etc.). Of course, for a small example program, the memory leak isn't going to hurt that much anyway [smile]

The question of when to call delete is made much easier if you are allowed to differentiate between objects and nodes. In essence, an object is an instance of the type your list stores, and the list is composed of nodes, each of which wraps an object. Now the client code is responsible for calling both new and delete on objects, and the list itself is responsible for calling new and delete on nodes (upon insert and remove).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by Chrono1081
If you call new, and don't call a matching delete later on, then by definition you have a memory leak.


Though it can be hidden from "userspace-code" (you as the user of some library) when using smart pointers, like the ones from boost (http://boost.org). The delete/delete [] is of course still there, or better, it is already there. Most often you don't have to worry (as long as you don't have [indirect] cyclic references, then a weak_ptr will help you).


If debugging the instantiation of interface classes, then the following sometimes helps me to test a particular configuration:

template <bool debug=true> struct Mom;template <> struct Mom <true> {    Mom () { ::std::cout << "Constructed " << this << ::std::endl; }    virtual ~Mom () { ::std::cout << "Destructed " << this << ::std::endl; }    ...    <interface functions>};template <> struct Mom <false> {    Mom () {}    virtual ~Mom () {}    ...    <interface functions>};class Son : public Mum {   ...   <implement interface functions>};


This helped me to debug an abstract syntax tree the last days.
Quote:Original post by SiCrane
The easiest way to do basic leak checking in MSVC is to use the CRT debug functions. In this case you'd probably want to use _CrtSetDbgFlag() to create a leak check on program exit. Ex:
#include <crtdbg.h>int main(int, char **) {  _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );  // do stuff}


Hey, sorry to hijack the topic, but is there a similar method for doing this using GCC?
View my blog! All about programming (C#, .NET, C++)!www.spikedsoftware.co.uk
@JapanFred:

Have a gander at the System.Diagnostics namespace if using C# loads of listeners and debug routines kindly provided :)

I believe you can also get hold of the GC through there which is mighty handy.
"I have more fingers in more pies than a leper at a bakery!"
gcc by itself doesn't contain memory leak checking functionality. That is deferred to the C runtime implementation used. On most *nix boxes this will be glibc, which has allocation debugging through memory tracing.
Quote:Original post by JapanFred
Hey, sorry to hijack the topic, but is there a similar method for doing this using GCC?


Behold!
Quote:Hey, sorry to hijack the topic, but is there a similar method for doing this using GCC?


If you're on Linux you can also just run it through Valgrind. It doesn't require any source code changes, I think it just needs debugging symbols.

This topic is closed to new replies.

Advertisement