3

I know that Visual Studio allows you to compare memory between two time snapshots in order to find leaks, using the debugger-integrated Memory Usage diagnostic tool. However is there a way to filter out of the diff any memory that was allocated after another time point (B) between start time (A) and end time (C) ?

Time A = start caring about memory that gets allocated

Time B = stop caring

Time C = all memory that was allocated between Time A and Time B should now be freed; if not, let's see the callstack that allocated each chunk

This does not necessarily need to be done using Visual Studio interactive diagnostics tool if is there for example a way to do this using _CrtMemCheckpoint instead or another way.

Although I have tagged this as a Visual Studio 2019 question, I will accept any solution that uses freely available Microsoft tools such as WinDbg or Free Open Source tools such as VLD (Visual Leak Detector) to achieve the same result.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • Probably, stupid question... Do you mean - only dynamic memory allocation (explicitly by "new") or including all other allocation? – Lunch Basketball Jun 02 '22 at 07:26
  • @LunchBasketball all heap allocated memory. so there is a "new" somewhere but it may be hidden inside standard library code rather than "explicit." – Patrick Parker Jun 03 '22 at 01:39
  • Overloading new/delete is trivial. Having a global flag that start/stops the trace is trivial. The complex part getting a hashtable with stack traces and addresses of allocation. I'll look if there's a library somewhere. – vpa1977 Jun 03 '22 at 10:23

1 Answers1

0

Assuming the target platform is Windows since VS2019 is mentioned, I've found a tool similar to what you are looking for. https://www.codeproject.com/Articles/11221/Easy-Detection-of-Memory-Leaks

It is pretty old but still compiles.

Code from MemoryHooks can be used for new/delete override implementation. (void* operator new (std::size_t count ); and void operator delete (void* ptr);)

vpa1977
  • 326
  • 1
  • 5
  • The single process limitation is a slight annoyance, but this should still be useful. And it does meet all my criteria set for the bounty. Thanks. – Patrick Parker Jun 04 '22 at 03:24
  • Also, the comments suggest that it doesn't work for 64 bit Windows and it links to a fork which presumably fixes that. https://sourceforge.net/projects/diagnostic/ (looks a bit shady t.b.h. since the fork requires some "password" and acknowledgement of the author, but no mention of the original author) – Patrick Parker Jun 04 '22 at 04:28
  • The main thing should be ripped out of it - map implementation that does not use STL - stack capture is described in many places, e.g. here https://stackoverflow.com/questions/590160/how-to-log-stack-frames-with-windows-x64 – vpa1977 Jun 04 '22 at 10:44