How can I list all instantiated objects in all application, using FASTMM4 or default memory manager?
-
Why do you want this information? Are you looking for memory leaks? I think there can be another way then. – Alex Jun 23 '09 at 08:11
-
@Alexander: No at all man! FASTMM4 shows memory leaks easily, in truth, I wanted to know if a specific class at VCL was loaded and who instantiated it, but now I just by pass this question. Thanks! – Gedean Dias Jun 23 '09 at 10:20
5 Answers
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.

- 82,511
- 50
- 270
- 477
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.

- 10,997
- 11
- 73
- 124
-
-
Fine! Should work, but I think change VCL defaults is the last option, but if there's no else solution, I'll do that. Thanks. – Gedean Dias Jun 22 '09 at 11:05
-
@gabr: If the list is a global, he can.. What he does not can with a helper is creating instance data... – Fabricio Araujo Jun 22 '09 at 23:03
-
1@Fabricio tried it, not possible, afterconstruction cannot be overriden with a class helper, it does nothing. What might be possible though is implemented a virtual constructor... and add some code to the virtual constructor ! Interesting ! One problem remains though, if it's supposed to be removed from a list... then same problem again... but at least got you half way there. However all Delphi code probably calls the static constructor, so it might not still be of much use... not sure if static or virtual constructor has priority... hmm... – oOo Jan 30 '22 at 23:04
-
Existing virtual methods cannot be overriden with class/object helpers, however a virtual constructor Create can be added, and it seems it takes priority over the static one ! LOL. So at least it's possible to add all TObjects to a global list ! ;) From there it should be possible to get RTTI information via .ClassInfo or so. – oOo Jan 30 '22 at 23:12
-
However there might still be a problem and there probably is. Self is not yet stable inside the Tobject.constructor :( it's probably nil hehe... that is why AfterConstruction is needed even this might still be nil for some generics or something. – oOo Jan 30 '22 at 23:14
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.

- 2,021
- 21
- 25
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.

- 5,477
- 2
- 36
- 56
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.