This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Problem with list.
- From: John Love-Jensen <eljay at adobe dot com>
- To: Aseem Rastogi <aseem at india dot tejasnetworks dot com>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Fri, 23 Sep 2005 06:27:12 -0500
- Subject: Re: Problem with list.
Hi Aseem,
> Can somebody explain the following behaviour?
After you relinquish a pointer to l list, you continue to hold onto the
pointer in a.
When the A object in l list is deleted, the a pointer is left with a
dangling reference.
A dangling reference is not guaranteed to cause a segmentation fault when
dereferenced. It appears that the heap still had the "ghost" of the A
object, which the a pointer accessed -- but it could have also caused a seg
fault. The behavior is undefined.
There are several strategies you can employ to help you catch programming
mistakes like this.
One is to use a heap manager that scrubs all released memory to some garbage
value. 0xDEADBEEF is a common choice, since it is conspicuous in a
debugger. You probably wouldn't want that overhead in production code, but
for diagnostic purposes it can be quite handy.
Another strategy is to use managed pointers, such as std::auto_ptr or one of
the various managed pointed in Boost, which will explicitly relinquish
ownership.
Another strategy is to use a third party product to help detect this kind of
situation. Such as Electric Fence, ValGrind, IBM Rational Purify &
Quantify, or Nu-Mega Technologies Bounds Checker... just to name a few.
There are a lot out there.
q.v. http://www.sdmagazine.com/jolts/prev_utl.htm
HTH,
--Eljay