views:

268

answers:

4

How can I list all instantiated objects in all application, using FASTMM4 or default memory manager?

+3  A: 

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.

Mason Wheeler
+1  A: 

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.

Toby Allen
No, you cannot use helper object for that.
gabr
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
@gabr: If the list is a global, he can.. What he does not can with a helper is creating instance data...
Fabricio Araujo
A: 

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.

David Taylor
A: 

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.

Alexander