This is the mail archive of the gdb@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Associating C++ new with constructor


Ross Boylan wrote:
This is NOT about the problems setting a breakpoint on a C++
constructor!

I've been looking for a way to count creations and destructions of C++
objects, with the counts kept on a per class basis.
Many (9?) years ago I have written a dumb perl script to do this task. It is
still being used from time to time, despite valgrind's massif.
I can't give you the script, without considerable paperwork, since
it is (C) IBM (and I won't try because it is too embarrassing to
release such a trivial script).


The idea is simple, and took me less than a day to implement:
Define a template class that can monitor object creation and
destruction.
Insert a member of this type into all non-POD classes:
class MyFoo {
....
....
...
Count<MyFoo> m_count_for_MyFoo;
};

This member will be constructed every time your MyFoo is
constructed. Inside the Count class you can do whatever
you want, every Count<T> class may update a global
registry, from which you can print statistics for all Count
classes.

Note that the perl script simply detects "class ....." and
inserts the members into it. The nice thing about this
template thing, is that it should work well even
if your MyFoo is also templated, and it works
well for all possible constructors.

It is imperfect since it can't reliably detect all
non-POD classes, and it does not work for std::***
classes. To make it 100% reliable, I had to write
a complete C++ parser!.

--
Michael Veksler
http:///tx.technion.ac.il/~mveksler


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]