1

My program detects a memory leak when closed and I'd like to resolve it. I've replaced my only usage of new with a smart pointer, but I am using quite a few bare char[] and can't really afford the time to go through each pointer I'm using in the project.

heap snapshot & memory leak

  • Can I identify the cause of a memory leak from this heap snapshot?
  • Is there a way to download this file and search through it for that particular memory location?
  • Am I wasting my time trying to diagnose this issue because Windows will clean up the memory anyways?
Skewjo
  • 379
  • 3
  • 12

1 Answers1

1

Can I identify the cause of a memory leak from this heap snapshot?

Typically not. You would need at least 2 snapshots and compare them to find the difference. You would then walk through the code that was run between these 2 snapshots and see where the difference comes from.

This requires a lot of discipline and closely following the steps of memory analysis:

  1. Perform the steps in question once. This will ensure that any lazy initialization is done.
  2. Take a snapshot
  3. Perform the steps in question again. Make sure you return to the same point as before.
  4. Take a snapshot
  5. Compare the snapshots

The result may still include false positives, e.g. output in the log window (20 lines before, 40 lines after).

Is there a way to download this file and search through it for that particular memory location?

I don't know if that's built into Visual Studio. But you could of course create a crash dump file with full memory and then look it up there.

Am I wasting my time trying to diagnose this issue because Windows will clean up the memory anyways?

IMHO you're not wasting your time. An application may crash and cause data loss for the user if it has a memory leak and runs out of address space. You'll find the problem and become a better programmer. You'll learn something which helps you also with other programming languages.

But other than that, you're right: Windows will free the memory when the process terminates. So restarting the program will likely help and that may do as a workaround for customers for a period of a few months.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Thanks for the quick reply. From your answer it sounded like you believe I have an on-going memory leak, but as far as I can tell the memory leak is only occurring when I close the application. I'm giving Deleaker a shot at the moment and I guess I'll see how it goes. – Skewjo Feb 16 '21 at 13:00
  • 1
    @Skewjo: sorry, I didn't get the fact that the application is about to terminate from the original question. Chances are higher that these are false positives. Perhaps you want to try [this approach](https://stackoverflow.com/a/8544704/480982) instead of introducing a new tool. – Thomas Weller Feb 16 '21 at 13:57
  • 1
    Thanks for the link to the other question. I ended up getting the new tool installed and it works pretty well and kind of helped me wrap my head around why the heap snapshot would not work to ID the leak. It brought up a staggeringly high number of memory leaks after closing the program, but the [consensus (about the first leak at least) seems to be that static execution in an external library can be safely ignored. ](https://stackoverflow.com/questions/66225765/memory-leak-of-sslcontext-using-boostbeast?noredirect=1#comment117085385_66225765) – Skewjo Feb 16 '21 at 14:53