Memory leaks detection

Suddenly noticed that while running, memory consumption is slowly but steadily creeping up:

which can only mean one thing:

We have a "memory leak".

Well, then we need to find (detect) and fix it (or them). Some sort of memory leak detection tool would help.

  • Just in case, we are talking about Windows environment here.

Visual Studio offers it's own set of "CRT debug heap functions". However, I was unable to get it to work. Then I found

Visual Leak Detector

by KindDragon. You can download it here. It's free. Works with VS 2022 just fine. Easy to install and to use. However, couple suggestions:

Installation. Though everything is pretty straight forward, still:

  • In order to keep your OS cleaner, better to change install location from default to your project, In my case - to C:\CPP\p_windows\Visual Leak Detector
  • Don't need to create a Start Menu Folder
  • Don't really need to modify your environment, so UNcheck all 3 "Add VLD directory" options. Instead, we can specify DLL path in Project properties.

Deployment. We have to specify in the Project where to look for DLL and LIB libraries and for VLD header:

  • .LIB: Go to Project Properties -> Linker -> General. Configuration: DEBUG. Additional Library Directories -> Edit. Add New Line:
..\..\p_windows\Visual Leak Detector\lib\Win32

(or 64? depending on your project)

Ok - Apply - Ok

  • .DLL: Go to Project Properties -> Configuration Properties -> Debugging. Configuration: DEBUG. Environment -> arrow down -> Edit. Add a line:
PATH=..\..\p_windows\Visual Leak Detector\bin\Win32

Ok - Apply - Ok

  • In main.cpp on the very top, before everything else, add
#include "../../p_windows/Visual Leak Detector/include/vld.h"

Build and run.

  • Just in case: Since now VLD registers and records all memory allocations, the initialization will take MUCH longer than usual, so be patient.

Then stop. If you have memory leaks, they will be reported as

The beauty of VLD is that it shows specific line where leacked memory was allocated plus full call stack. Now, all you need - is to figure out where in your code you supposed to release it and why you didn't.

In my case, despite all expectations, instead of couple minor accidentally overlooked leaks, it revealed me a bunch (around 10) of serious logical discrepancies...

Took 2 days to fix them all. But now - No memory leaks detected.


Leave a Reply

Your email address will not be published. Required fields are marked *