• Advertisement
Sign in to follow this  

How can I locate a memory leak?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've seen many questions about this, however most if not all responses just tell you how to fix them or how to avoid them and not how to actually locate it.

 

----------------------------------------------------------------------------------------------------------------

 

What I would like to know is :

 

  • How do I locate a memory leak? Which class, function or even line of code it could be in.
  • What could cause it? (Not sure how wide this question is...)
  • How can I prevent a memory leak in the future?

 

Share this post


Link to post
Share on other sites
Advertisement
I also recommend valgrind, because not only does it detect leaks, it can detect many other memory errors like writing unallocated memory, use after free, off by one, etc.

It can be a faff to run under Windows, but the effort is worth it, it's completely free too...

Share this post


Link to post
Share on other sites

I've mostly done it the old fashion way. Basically... in C, you'll need an intermediate function that can record data to a database. When a leak is detected, you'll need to crash as soon as possible. So you have the program write out it's current processing stack (because Visual Studio is really F*CKING screwy about this). There are libraries that will assist with this.

You then also export your allocation information. You could also have allocations be updated live via TCP connection to an HTML document.

In C++ you'll overload the new and delete operators with similar functionality. But the best bet is generally to use custom allocators who's job is to track that mess.

If you read your stack, and it shows that you stopped at accessing data. It means your leak was a segmentation fault.

If your allocator shows abnormally large amounts of memory being used for certain data types. Then you'll need to start logging the allocations and deallocations. You can build a macro to help you with this that will also record the line number and the file.

Share this post


Link to post
Share on other sites

If you're using VS
 
https://vld.codeplex.com/


Actually, you don't need any third party tool; VS (through the C runtime library) has leak detection built it. See: https://msdn.microsoft.com/en-us/library/x98tx3cf.aspx

 

The tool Khatharr points too provides better leak diagnosis than the built-in solution. So definitely also worth checking out.
 

Honestly, if you just learn to use RAII then you don't have to mess with it much anymore.

 

Good advice, but I've certainly managed to leak memory even when using RAII (incorrectly, obviously). Sad to say, you should learn how to track down memory leaks if you want to be a well-versed C++ developer.

Share this post


Link to post
Share on other sites

 

 

Honestly, if you just learn to use RAII then you don't have to mess with it much anymore.

 

That's like saying that if carpenters would only learn to use hammers then they wouldnt need to mess with screwdrivers anymore

Share this post


Link to post
Share on other sites

 

 

 

 

Honestly, if you just learn to use RAII then you don't have to mess with it much anymore.

 

That's like saying that if carpenters would only learn to use hammers then they wouldnt need to mess with screwdrivers anymore

 

 

It's more like saying that if you aren't a jackass then you don't have to learn how to avoid spilling your beer while you're driving.

 

RAII is a discipline that specifically targets resource leakage and nearly solves the issue by itself, whereas using a hammer on a screw is just using the wrong tool for the job. If you're not using RAII and you have a ton of leaks then fucking use RAII and stop wasting everyone's time and energy on solved problems. If you are using RAII then you should already know where the leak is coming from (the place where you did the weird shit), and if not then there have been tools suggested to help you out.

 

 

Hammer/nails are tools for attaching two materials together, whereas screws/screwdrivers... are also tools for attaching two materials together.

 

RAII is one way of dealing with memory allocation, but it's not the only one and it's not appropriate in all cases.  Hammers/nails are one way of attaching two materials together, but not the only one.  There are cases where you need to use screws/screwdrivers, or glue, welding, tape, etc.  You're suggesting that all I need is this one technique to deal with memory allocation and deallocation, which is not true any more than I can use nails and a hammer to attach anything and everything together.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement