Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-01-07 11:23:50


From: "William E. Kempf" <wekempf_at_[hidden]>
>
> I think that extending it to free memory in cycles would be a great idea.
Of course, this opens a large can of worms about how to handle destruction
of objects which refer to each other...<

One possible approach is to return a list of references to the "offending"
shared_ptr instances to the user, who can then reset() them as appropriate.
Actually it's not as simple as that since a reset() can invalidate some of
the returned references; the shared_ptr instances would first need to be
copied to a temporary container, the originals reset(), and the temporary
container destroyed, but the general idea is the same.

> I think that full GC is one of the major things missing from the C++
language, and the appeal of a smart pointer approach, where I pay for GC
only when I want/need it, is a great idea.<

Making a real "zero overhead" collector is a bit harder, and I'm not sure I
have the time to do it.

The main requirement is the ability to discover all active counted_base
objects; currently I maintain a global std::map of their addresses, and
that's probably not acceptable for production code. A dedicated allocator
that provides the ability to enumerate allocations may help here.

An additional requirement is to be able to discover shared_ptr subobjects
and to distinguish a shared_ptr from a weak_ptr; I've used a "magic
constant" but this increases sizeof(shared/weak_ptr) from 8 to 12 on a
typical implementation. It is possible to "mangle" the object pointer (by
XOR-ing it, for example) in weak_ptr and replace the magic constant test
with a valid pointer test, but I'm not sure whether this is a good idea. :-)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk