oops, corrupted memory again!

KW Heuer kwh at bentley.UUCP
Fri May 9 23:39:03 AEST 1986


In article <5097 at think.ARPA> rose at think.ARPA (John Rose) writes:
>If you do have source code, here's another suggestion which has worked
>very well for me.  Define an circular buffer which stores a record of
>the last few hundred records of malloc/free/morecore history.  Make
>sure your symbolic debugger can dump it for you.  This trick alone
>has saved me hours of debugging time on quite a few occasions.

I've found the following front-end* to be very useful:
	char *Dalloc(n) unsigned n; {
	    register char *p = malloc(n);
	    fprintf(stderr, "+%8x\n", p);
	    return (p);
	}
	void Dfree(p) char *p; {
	    fprintf(stderr, "-%8x\n", p);
	    free(p);
	}
	char *Drealloc(p, n) char *p; unsigned n; {
	    fprintf(stderr, "-%8x\n", p);
	    p = realloc(p, n);
	    fprintf(stderr, "+%8x\n", p);
	    return (p);
	}

When the program terminates (normal exit or core dump), I run a consistency
check on the log file to cancel out "+xxx" and "-xxx".  Any unbalanced "-"
is a bug.  I consider an unbalanced "+" to be a bug, too, unless there are
a bounded number of them and I can account for them all.

Karl W. Z. Heuer (ihnp4!bentley!kwh), The Walking Lint
*It can also be built into malloc.c if you have source; this allows it to
log the calls within stdio.  For some applications, a special log file
should be used instead of stderr.



More information about the Comp.lang.c mailing list