I want to troubleshoot memory leaks in a C++ native application that uses the C++ Run-Time Library. Can I easily know the number of currently allocated blocks (and optionally the total size) ?
Asked
Active
Viewed 200 times
1
-
1What are you trying to do? Do you want to find the leaks, or do you just want to count how many blocks have been allocated? Because the latter won't be very much use if you want to do the former. – David Heffernan Jun 24 '19 at 13:55
-
1The CRT has [Tools to do this](https://learn.microsoft.com/en-us/visualstudio/debugger/crt-debug-heap-details?view=vs-2019) assuming you're using the debug heap – Mgetz Jun 24 '19 at 13:58
-
That's like trying to find out if someone is stealing money from you by inspecting your account balance. Sure, if someone does, it will be reflected there. But it doesn't look any different from regular account activities. – IInspectable Jun 25 '19 at 07:21
-
@IInspectable: what ??? When you have a leak, the number of allocated blocks keeps growing, and the total size allows to estimate the leak size. – Jun 25 '19 at 07:32
-
Likewise, if your application doesn't leak memory, the number of allocated blocks can keep growing. Now let's assume for a moment, that this never happened, and that you *do* have a memory leak. What next? Answering the yes/no question hasn't gotten you any closer to isolating the issue. – IInspectable Jun 25 '19 at 07:52
-
@IInspectable: no, the amount of non-leaking blocks remains bounded. You spot the leak by skipping parts of the code to isolate the offending one. Note that I just found my leak this way, in about an hour. It seems that you adopt the "I don't want this to be true" attitude. – Jun 25 '19 at 08:01
-
Not quite. I follow the Marsellus Wallace philosophy here: *"We don't wanna think. We wanna know."* So instead of writing code that samples largely uninteresting scalar values, compare them, fall victim to false assumptions, and *possibly* find the issue, by chance, I would have just used the Debug Heap facilities, and have the application report memory leaks to me *on every run*. That report includes the file and line of code for every memory allocation that wasn't released. Maybe we just have different ideas of what comprises a directed problem solving approach. – IInspectable Jun 25 '19 at 08:10
-
@IInspectable: irony well received. – Jun 25 '19 at 08:12
3 Answers
2

Michael Chourdakis
- 10,345
- 3
- 42
- 78
-
-
Actually, it helped me observe that there was no leak in the heap (new/delete were balanced). The fault was in other resource deallocation. – Jun 25 '19 at 20:37
1
You could try Massif from Valgrind.
Massif is a heap profiler. It measures how much heap memory your program uses. [...] Also, there are certain space leaks that aren't detected by traditional leak-checkers,

Paul Evans
- 27,315
- 3
- 37
- 54
0
On windows, you can use Dr Memory, on linux valgrind. Can also use leak sanitizer with gcc on linux or clang with -fsanitize=leak , but you will also nead some runtime support (you need to link to some other libs for gcc or clang sanitizers).

nicolasb565
- 78
- 5