如何列出所有实例化对象?
如何使用 FASTMM4 或默认内存管理器列出所有应用程序中的所有实例化对象?
How can I list all instantiated objects in all application, using FASTMM4 or default memory manager?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您也许可以使用 FastMM4 来完成此操作,但这会很复杂。 尝试检查 ScanForMemoryLeaks 过程的代码,看看它是如何完成的。
此例程根据预期内存泄漏列表检查所有分配的堆内存,并报告显示的所有内容,包括计数和对象类名称(如果找到对象)。 您想要做的是检查所有分配的堆内存并报告您找到的所有对象的计数和对象类名称,因此这是一个非常相似的任务。 只需省略注册指针列表检查并过滤掉所有不是对象的内容即可。
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.
嗯,它不支持也不鼓励,但根据您的情况,您也许可以编辑 TObject 来记录创建和销毁 - 因为所有对象都是 TObject 这可能会成功。 然而,修改 VCL 确实令人不悦,所以我想知道您是否可以使用 Helper 对象(我认为自 2006 年以来可用)来完成同样的事情。 将创建的每个对象的详细信息写入另一个列表等。
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.
实现 Tony Allen 建议的一个有趣方法可能是在运行时挂钩对象创建和销毁方法。 Google Code 上有一个有趣的库,它是 AsmProfiler 项目的一部分(恕我直言,这是一个非常好的工作),可能可以解决这个问题。 以下是 KOLDetours 单元的链接这完成了所有繁重的工作。
FWIW 我在 Delphi 4/5 天实现了一个框架,其中包含一个检测基类,可以选择跟踪对象创建/销毁。 这对于追踪对象泄漏等非常有效,但会产生大量信息。 如果您只需要知道关闭时泄漏了哪些对象,那么 FASTMM4 是一个更好的选择。
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.
您可以通过调用 SetMemoryManager 来更改内存管理器。
您可以编写自己的 MM,这将是一个简单的存根:它将所有调用重定向到旧 MM(即 FastMM,您可以通过调用 GetMemoryManager 获取它)并在某处记录所有内存操作。
您可以通过查看调用堆栈来检测对象创建/销毁:应该从 TObject 的 NewInstance 方法进行调用。
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.
这是一个应该有所帮助的想法,尚未完全尝试,但它会让您走得更远:
按以下方式编写类助手:
使用构造函数编写通用 Tobject 类助手。
为覆盖其构造函数的类编写自定义/特定类帮助器。
使用自由方法编写通用 Tobject 类帮助器。
为覆盖其自由方法的类编写自定义/特定类帮助器。
最后检查所有代码是否调用了.Free,如果没有则将其从.Destroy更改为.Free。 如果您希望从列表中删除所有对象。
在您想要列出的每个单元中包含带有类助手的单元。
同时在该单元中创建全局列表。
在构造函数中只需编写
继承的Create;
GlobalObjectList.Add( 自身 );
在自由方法中写入:
GlobalObjectList.Remove( Self );
继承免费;
这可能不是线程安全的,并且对于使用泛型或泛型方法进行的某些初始化可能会失败,泛型似乎仍然有些问题,因此您的里程可能也与关键部分和/或线程相关。
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.