This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
Re: Associating C++ new with constructor
- From: Michael Veksler <mveksler at tx dot technion dot ac dot il>
- To: Ross Boylan <ross at biostat dot ucsf dot edu>
- Cc: gdb at sourceware dot org
- Date: Thu, 15 Feb 2007 15:35:14 +0200
- Subject: Re: Associating C++ new with constructor
- References: <20070215073516.GW5871@wheat.betterworld.us>
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