[Gc] CG and std::vector

Boehm, Hans hans.boehm at hp.com
Thu Jun 17 14:02:45 PDT 2004


If you use the GC with an STL collection, and the STL collection
contains pointers to garbage-collected objects, you should not use
the default allocator for the STL collection.  The collector needs
to know about the memory used directly by the collection class, so
that it can trace it and keep track of the objects it refers to.

The most portable way to do this is to include "gc_allocator.h" from
the GC distribution, and then to instantiate a vector with
something like

vector<Entity *, gc_allocator<Entity *> > entityVector;

If the vector itself will be explicitly destroyed and doesn't need
to be collected, use

vector<Entity *, traceable_allocator<Entity *> > entityVector;

If that doesn't completely solve your problem, please post a stack
trace and more details about the platform.

I doubt that Borland's tools deal correctly with the presence of
the garbage collector.  They would have to understand that the
collector implements its own memory allocator, etc.
You should not have to do the explicit cleanup.

Hans

> -----Original Message-----
> From: gc-bounces at napali.hpl.hp.com
> [mailto:gc-bounces at napali.hpl.hp.com]On Behalf Of Miguel Guinada
> Sent: Thursday, June 17, 2004 4:02 AM
> To: gc at napali.hpl.hp.com
> Subject: [Gc] CG and std::vector
> 
> 
> Grettings,
> 
> Does the GC perform well deallocating memory inside an std::vector?
> 
> I have a std::vector wrapped in a (class something like this):
> 
> class Collection : public gc {
>   

> }
> 
> class EntityVector : public Collection {
>    vector<Entity *> entityVector; //NOTE: the class Entity 
> inherits from
> class gc 
> 
>    void cleanUp();
>  public:
>    void add(Entity *e)  {
>      entityVector.push_back(e);
>    }
> 
>    void remove(Entity *e);
> 
>    //etc.
> }
> 
> When I destruct this class I execute cleanup:
> 
> void EntityVector::cleanUp() {
>   vector<Entity *>::iterator it = entityVector.begin();
> 
>   for(; it != entityVector.end(); it++) {
>     delete *it;
>     *it = NULL;
>   }
>   entityVector.clear();
> }
> (I do the same for a single element when I call remove())
> 
> 
> 
> But it seams that GC is trying to delete a pointer that 
> already was deleted
> by my cleanup method and I get an AccessViolation! That or I'm doing
> something wrong!
> 
> But if I don’t do my cleanup Borland’s Code Guard (memory 
> profiling tool)
> reports an resource leak! But I’m not sure that Code Guard is 
> correct! Maybe
> it reports the resource leak before GC does it’s job? 
> 
> I would like some advice on what to do! As well to ask if 
> there is any tools
> to monitor issues like this (I not getting along with CodeGuard)! 
> 
> 
> Thanks very much!
> 
> Best Regards
> 
> Miguel Guinada
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.707 / Virus Database: 463 - Release Date: 15-06-2004
>  
> 
> _______________________________________________
> Gc mailing list
> Gc at linux.hpl.hp.com
> http://www.hpl.hp.com/hosted/linux/mail-archives/gc/
> 


More information about the Gc mailing list