Reputation: 1103
How can I list all instantiated objects in all application, using FASTMM4 or default memory manager?
Upvotes: 1
Views: 1322
Reputation: 291
Here is an idea which should help, not fully tried, but it will get you far:
Write class helpers in the following way:
Write a generic Tobject class helper with a constructor.
Write custom/specific class helpers for classes which override their constructor.
Write a generic Tobject class helper with a free method.
Write custom/specific class helpers for classes which override their free method.
Finally check all code if it calles .Free if not change it from .Destroy to .Free. If you want all objects removed from a list.
Include the unit with the class helpers in each unit which you want listed.
Also create the global list in that unit.
In the constructors simply write
inherited Create;
GlobalObjectList.Add( Self );
In the free methods write:
GlobalObjectList.Remove( Self );
inherited Free;
This may not be thread-safe, and it might fail for some initializations with generics or generic methods, generics still seem somewhat buggy here and there so your mileage may very also in relation to criticalsections and/or threads.
Upvotes: 0
Reputation: 5658
You can change memory manager by calling SetMemoryManager. You can write your own MM, which will be a simple stub: it will redirect all calls to old MM (which is FastMM, you can get it by calling GetMemoryManager) and log all memory operations somewhere. You can detect object creation/destruction by looking at call stack: call should be made from TObject's NewInstance method.
Upvotes: 0
Reputation: 2051
An interesting way to implement Tony Allen's suggestion might be to hook the object creation and destruction methods at runtime. There is an interesting library on Google Code that is part of the AsmProfiler project (a very nice bit of work IMHO) that might do the trick. Here is the link to the KOLDetours unit that does all of the heavy lifting.
FWIW I implemented a framework back in the Delphi 4/5 days that contained an instrumented base class which optionally tracked object creation/destruction. This worked well for tracking down object leaks and such, but generated an enormous amount of information. FASTMM4 is a much better option if all you need to know is what objects have leaked at shutdown.
Upvotes: 0
Reputation: 11211
Well its not supported and not encouraged, but depending on your situation you might be able to edit TObject to record creation and destruction - since all objects are TObject this might do the trick. However modifying the VCL is really really frowned upon, so I wonder could you use a Helper object (available since 2006 I think) to do the same thing. Write to another list etc with details of each object created.
Upvotes: 1
Reputation: 84650
You could probably do this with FastMM4, but it would be a complicated. Try examining the code for the procedure ScanForMemoryLeaks to see how it's done.
This routine checks all assigned heap memory against the list of expected memory leaks and reports everything that shows up, including a count and the object class name if it finds objects. What you want to do is check all assigned heap memory and report the count and object class name of all objects you find, so it's a really similar task. Just omit the registered pointer list check and filter out everything that's not an object.
Upvotes: 3