mcheck/mtrace for c++
ahoward
ahoward@fsl.noaa.gov
Wed Feb 6 11:57:00 GMT 2002
all-
By using #include <mcheck.h> and the mtrace() call one can obtain very
usefull diagnostics about dynamic memory usage :
Memory not freed:
-----------------
Address Size Caller
0x08049eb8 0xb0f2 at /home/ahoward/eg/c/leaky.cc:15
0x08060ee8 0x4 at /scratch/gcc-3.0/i686-pc-linux-gnu/libstdc++-v3/libsupc++/new_op.cc:48
however, the second line shows this does not work with c++, since only the
call to malloc from within new is shown. i'm trying to implement
something similar for c++ and have attempted to use the following code to
register a new_handler, it comipiles fine and runs as well - but the
handler does not get registered (output does not show up). Any help is
appreciated and apologies all round if this is not an appropriate list to
mail this item to.
source included at bottom and attached.
-a
--
====================================
| Ara Howard
| NOAA Forecast Systems Laboratory
| Information and Technology Services
| Data Systems Group
| R/FST 325 Broadway
| Boulder, CO 80305-3328
| Email: ahoward@fsl.noaa.gov
| Phone: 303-497-7238
| Fax: 303-497-7259
====================================
SOURCE FOLLOWS
=====================================
#include <stdio.h>
#include <stdlib.h>
/* would be <leak_debug.h> */
#ifndef LEAK_DEBUG_H
#define LEAK_DEBUG_H
#include <new>
extern std::new_handler __new_handler;
void my_new_handler(void);
std::new_handler org_handler = (std::new_handler) NULL;
#endif /* #define LEAK_DEBUG_H */
void my_new_handler(void){
printf("my_new_handler called...\n");
org_handler();
}
class obj{
public:
int var;
obj(void){ printf("obj() called...\n"); }
~obj(void){ printf("~obj() called...\n"); }
};
int main(int argc, char **argv){
org_handler = std::set_new_handler(my_new_handler);
/* just to be sure */
__new_handler = my_new_handler;
obj *o = new obj();
o->var;
int *a = new int[1];
return 0;
}
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
/* would be <leak_debug.h> */
#ifndef LEAK_DEBUG_H
#define LEAK_DEBUG_H
#include <new>
extern std::new_handler __new_handler;
void my_new_handler(void);
std::new_handler org_handler = (std::new_handler) NULL;
#endif /* #define LEAK_DEBUG_H */
void my_new_handler(void){
printf("my_new_handler called...\n");
org_handler();
}
class obj{
public:
int var;
obj(void){ printf("obj() called...\n"); }
~obj(void){ printf("~obj() called...\n"); }
};
int main(int argc, char **argv){
org_handler = std::set_new_handler(my_new_handler);
/* just to be sure */
__new_handler = my_new_handler;
obj *o = new obj();
o->var;
int *a = new int[1];
return 0;
}
More information about the Libstdc++
mailing list