C++:如何在运行时计算所有实例化对象的数量?
我有一个由许多 C++ 类组成的大型框架。有没有办法在运行时使用任何工具来跟踪正在构造和当前存在的所有 C++ 对象?
例如,在某个时间 t1,也许应用程序有对象 A1、A2 和 B3,但在时间 t2 时,它有 A1、A4、C2 等等?
这是一个跨平台框架,但我熟悉在 Linux、Solaris 和(可能)Mac OS X 中的工作。
I have a large framework consisting of many C++ classes. Is there a way using any tools at runtime, to trace all the C++ objects that are being constructed and currently exist?
For example, at a certain time t1, perhaps the application has objects A1, A2 and B3, but at time t2, it has A1, A4, C2 and so on?
This is a cross platform framework but I'm familiar with working in Linux, Solaris and (possibly) Mac OS X.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在要计数的对象的析构函数和构造函数中注入代码:
不要忘记增加所有构造函数(复制构造函数等)中的计数器
编辑:
然后:
自动为每种类类型生成一个计数器。
You can inject code in the destructor and constructor of the objects that you want to count:
Don't forget to increase the counter in all constructors (copy constructors, etc.)
EDIT: In this situation one can use the curiously recurring template pattern:
and then:
to automatically generate a counter for each class type.
我假设您只计算堆上的对象。如果是这种情况,您可以通过用一些自定义宏替换它们来记录所有对 new 和 delete 的调用。
我们开发了这样一个定制的日志记录工具。主要目的是跟踪内存泄漏,但它也可用于找出任何特定时间有多少对象。例如,
MY_LOGGING 在每行的开头自动添加时间戳。该行包含类名、文件名、行号、函数名和大小。
实用程序会解析日志文件并随时生成显示对象数量、使用的总大小等的图表。
当然,您必须用宏替换每个新的/删除的调用。这可能是一项相当大的工作。
I assume that you count only objects on the heap. If this is the case, you can log all calls to new and delete by replacing them with some custom macros.
We have developed such a custom logging facility. The primary purpose was to track memory leaks, but it can also be used to find out how many objects at any specific time. For example,
The MY_LOGGING adds timestamp automatically at the beginning of each line. The line has the class name, filename, line number, function name, and the size.
A utility parses the logging file and generates graphs showing the number of objects, total size utilized etc at any time.
Of course, you have to replace every new/delete calls with the macros. Which might be quite a bit work.
我自己没有使用过 Massif 可能是您正在寻找的工具。
http://valgrind.org/info/tools.html
I have not used it myself by Massif might be the tool you are looking for.
http://valgrind.org/info/tools.html
创建一个与 Java 的 Object 等效的特殊基类,并让每个类都派生自该基类。然后在该类中分别将 Andreas Brinck 建议的全局计数器操作放入构造函数/析构函数中。除了确保派生对象仅算作一个对象之外,这意味着您只需要检测 1 个构造函数和 1 个析构函数。当然,缺点是你需要稍微改变每个类的定义......
Make a special base class equivalent to Java's Object and make every class derive from that. Then in that class put the global counter operations Andreas Brinck suggests in the constructor/destructor respectively. In addition to making sure a derived object is only counted as one object, it means you only need to instrument 1 constructor and 1 destructor. Though of course the downside is you need to slightly change the definition of every class...
这仅适用于 Solaris,但如果可以选择,您可以使用 dtrace 来跟踪每个类的构造函数和析构函数调用的数量,并按一定时间间隔将其打印出来。这将需要大量的工作来设置所有进入/返回块,但我怀疑 dtrace 脚本可以自动生成。
This is Solaris only, but if that's an option, you can use dtrace to track the number of constructor and destructor calls for each of your classes and print it out at an interval. This will require an amount of work to set up all the entry/return blocks, but I suspect the dtrace script could be auto-generated.