[Gc] Changes to gc_allocator.h

David Jones dej at inode.org
Tue Aug 17 11:04:30 PDT 2004


On August 17, 2004 01:25 pm, Ben Hutchings wrote:

> I changed destroy() to clear the memory of the destroyed object if it is
> not known to be pointer-free.  This is important because containers can
> destroy elements without freeing the memory they live in; this is
> particularly true of std::vector.  Unless the memory is cleared this can
> result in excessive retention of memory.  Unfortunately the standard
> does
> not require containers to call the allocator's destroy() member so this
> doesn't help everywhere.

One thing I did in my code base is to create a pseudo-smart pointer
GCPtr<T> which is basically a wrapper around a T* but with the following 
features:

GCPtr<T>() initializes the wrapped pointer to NULL.
~GCPtr<T>() clears the wrapped pointer.

If you have a vector of these things, then the vector is required to call the 
destructor for each element (no matter what allocator is used) so this should 
solve THAT problem.

Also, this solution works to clean the stack of decommissioned pointers as 
well.  (Generally, stack churn should quickly overwrite old pointers but 
that's not guaranteed to happen.)




More information about the Gc mailing list