问题
I am trying to understand how heap consistency checking works in GNU C Library. I Refered http://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html#Heap-Consistency-Checking
Here is a sample program I have written. I expected as suggested in the manual if I run in gdb and call mcheck(0)
my custom abortfn
would be called. But it isn't getting called.
What am I missing here?
included necessary headers.
void *abortfn(enum mcheck_status status)
{
switch(status) {
case MCHECK_DISABLED:
printf("MEMCHECK DISABLED\n");
break;
default:
printf("MEMCHECK ENABLED\n");
}
}
int main()
{
printf("This is mcheck testing code\n");
int *a = (int *) malloc(sizeof(int));
*a = 10;
printf("A: %d\n", *a);
free(a);
return 0;
}
回答1:
Today, compiling with all warnings & debug info (gcc -Wall -Wextra -g
) then using valgrind is more convenient.
However, the very documentation that you are linking to says:
it is too late to begin allocation checking once you have allocated anything with malloc
So start your main
as
int main() {
mcheck(abortfn);
However, your abortfn
should return void
so code it as:
void abortfn(enum mcheck_status status) {
switch(status) {
case MCHECK_DISABLED:
printf("MEMCHECK DISABLED\n");
break;
default:
printf("MEMCHECK ENABLED\n");
} }
来源:https://stackoverflow.com/questions/28529620/how-to-use-mcheck-for-heap-consistency-check-in-gnu-c