On Thu, 15 May 2003, Chris Sheppard wrote:
> Is there any way to make valgrind detect that there's a cycle? IE we
> (like many others) have a reference counted smart pointer in our
> application library, which gets used all the time. As you might guess,
> you have to be careful to avoid cycles, else you create things that,
> once you get rid of the external handle, their existing handles to one
> another will prevent their deletion.
>
> I created a very trivial test of this, and it didn't seem to detect that
> I had leaked the 2 objects (A had nothing but a handle to B, B had
> nothing but a handle to A). I guess this is because there ARE valid
> references, but I'm just wondering if there's any way to get valgrind to
> detect this?
The leak detector works like this: at termination, it scans through all
addressible memory looking for anything that looks like a pointer. For
any remaining heap blocks, if there are any pointers to the start (or the
middle) of the block found during the scan, the block is considered
reachable (or possible lost). This is imperfect, because occasionally
you'll get a "fake" pointer to a lost block just from a coincidental junk
value, but it doesn't happen very often.
So that's why no leak was reported for your cycle -- the leak checker
found a pointer to each object, and considered them reachable.
Adding cycle checking would be possible -- ignore a pointer within heap
block A to heap block B if the only pointer to heap block A is within heap
block B -- but it would be difficult. At a guess, I'd say more difficult
than is worth it. I'd be happy for someone to prove me wrong, though.
N
|