• 13
• 15
• 19
• 27
• 9

# Is there an easy way to detect a memory leak?

This topic is 3359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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}

##### Share on other sites
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;   }};

##### Share on other sites
Quote:
 Original post by Chrono1081The 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).

##### Share on other sites
Quote:
 Original post by Chrono1081If 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.

##### Share on other sites
Quote:
 Original post by SiCraneThe 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 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?

##### Share on other sites
@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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by JapanFredHey, sorry to hijack the topic, but is there a similar method for doing this using GCC?

Behold!

##### Share on other sites
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.