Re: Memory corruption and Dump Stack trace



Memory damage bugs rank among the most difficult and intractable bugs that exist. There
is nothing good about them. The worst part is what you observed, that they are random and
hard to reproduce.

First, if this is happening post-deployment, the simplest method is to use DrWatson to
create a dump file. This would be one of the first things I would try. If it happens on
your development machine, you can use JIT (Just-In-Time) debugging to invoke the debugger
at the point of crash.

One way to deal with dangling pointers is to make sure that after every delete you set the
pointer to the thing you deleted to NULL. This doesn't help you if there are multiple
pointers, alas.

One thing I did some years ago (when I used to get these all the time) was create a
"honeypot" object, an intermediate object that represented my object. It's a bit ugly and
wasteful of storage, but what I did was convert all references of the form
thing->field
to
thing->honeypot->value->field
and when I did a free (not delete, since this was pre-C++ for me) I would do
free(thing->honeypot->value);
and set
thing->honeypot->value = NULL;
now I'd get a NULL-pointer access failure if I used the dangling pointer. My approach was
a bit more elaborate, in that I would require that no one ever actually call free
directly, but instead it would be
FreeThing(thing);
#define FreeThing(x) _FreeThing(x, __FILE__, __LINE__)
where
void _FreeThing(Thing * thing, char * file, int line)
{
thing->honeypot->freepoint.file = file;
thing->honeypot->freepoint.line = line;
free(thing->honeypot->value;
thing->honypot->value = NULL;
}

Now if I got a null-pointer access, the freepoint struct gave me the file and line.

Honeypots just accumulated, and were never freed, so the program would grow and grow, but
since this would reasonably quickly find the problem, it didn't matter. A bit of
finagling of macros and #ifdef _DEBUG meant that in the release version, the honeypot got
deleted also. Today in C++ I could hide a lot of that inside classes.

Another solution, often simpler, is simply to add the following to your OnIdle handler:
ASSERT(_heapchk() == HEAPOK);
(check the docs for the correct spellings here...I'm typing this from memory). That way
if there is any heap damage, you catch it early, insted of waiting for it to nuke you much
later.
joe


On Sun, 01 Jul 2007 11:55:54 -0700, "karen.b.lin@xxxxxxxxx" <karen.b.lin@xxxxxxxxx> wrote:

Hi everyone,

my application has danging pointer, either pointing at something
that's deleted, or the memory was used by someone else. It causes
crashes very randomly and thus hard to reproduce the crash. I
narrowed it down to memory corruption because it crashed at calling a
pointer->doubleValue ...

Is there a way to produce a stack dump in MFC? I want to see who was
trying to delete the object. If anyone has better solution in looking
at this, please help!

Thanks!
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: "Mastering C Pointers"....
    ... all means go ahead and dive right into the C language. ... Memory is a separate unit which just stores bits. ... A pointer at the hardware level _is an integer_. ... since loops make your logic more much ...
    (comp.lang.c)
  • Re: what is the purpose of C++ smart pointer
    ... pointer tracks the data it is referring to and updates itself ... following the changes of the memory it points to. ... How exactly will the smart pointer know that you moved the ... int * x = new int; ...
    (comp.os.linux.development.apps)

Loading