mcheck/mtrace for c++

Phil Edwards phil@jaj.com
Wed Feb 6 12:08:00 GMT 2002


On Wed, Feb 06, 2002 at 07:57:09PM +0000, ahoward wrote:
> By using #include <mcheck.h> and the mtrace() call one can obtain very
> usefull diagnostics about dynamic memory usage :

Be aware that's a glibc header/function, not a standard one.


> and apologies all round if this is not an appropriate list to
> mail this item to.

It isn't, but the answer is simple enough I'll post it here.


> extern std::new_handler __new_handler;

Things beginning with a double underscore are internal details.  Do not
use them yourself.  They are hidden.


>     org_handler = std::set_new_handler(my_new_handler);
> 
>     /* just to be sure */
>     __new_handler = my_new_handler;

The first line is beautifully correct, the second line is horrendously wrong.
Stay away from the implementation details; someday that name will change
and then your code will break in front of your customers.

The main problem is that you've misunderstood the purpose of new-handlers.
They only are called when 'new' cannot fulfill a memory request, usually
because you're out of memory.  New-handlers are /not/ called on every
invocation of 'new' (unless you only have about a dozen bytes free in
the system).

You want to replace 'new', not install a different new-handler.


Phil

-- 
If ye love wealth greater than liberty, the tranquility of servitude greater
than the animating contest for freedom, go home and leave us in peace.  We seek
not your counsel, nor your arms.  Crouch down and lick the hand that feeds you;
and may posterity forget that ye were our countrymen.            - Samuel Adams



More information about the Libstdc++ mailing list