[Gc] Question about weak references

Stephane Epardaud stephane.epardaud at inria.fr
Fri Dec 8 07:09:07 PST 2006


Boehm, Hans wrote:
> Not having run the code, it looks OK.  Note that this does not give you
> a way to notify any code, and this kind of weak pointer will be cleared
> even if it is referenced by objects that are unreachable, but have not
> yet been finalized.
>   
I don't need more notification than the link becoming NULL when the 
pointed data goes away.
And I don't care about finalization at this point, so great !

> In the multithreaded case, you should access the weak pointer while
> holding the allocation lock (GC_call_with_alloc_lock()), since the
> collector currently clears weak pointers after all mark bits have been
> computed and the world has been restarted.  Otherwise you risk turning a
> weak pointer into a strong pointer after the collector has already
> decided to reclaim the object.
>   
Hum, I'm not sure how to turn a weak pointer into a strong pointer then, 
I thought that using some code like:

void *data = weak_ptr->data;
if (data != NULL)
  process(data);

Would be sufficient, since I work on a (strong) copy of the weak pointer 
data.
Do I need to put a lock when copying the weak_ptr->data into a strong 
pointer ?


Thanks a lot!


More information about the Gc mailing list