Weak references.

Jason House jason.james.house at gmail.com
Mon Apr 14 08:50:06 PDT 2008


Bill Baxter Wrote:
> >     if( GC.getAttr( weakref ) & GC.BlkAttr.NO_SCAN )
> >         printf( "The GC will not scan weakref\n" );
> 
> Is that how it's supposed to work?  I mean WeakRef is basically
> 
> class WeakRef { size_t hidden_pointer; }
> 
> Does that mean Tango shouldn't be trying to scan instances of it?  I 
> just ask because I was thinking the GC *would* scan the WeakRef's memory 
> just not find anything that looks like a pointer there.

That's the intent.  It keeps a reference without the GC detecting it.  The only extra wrinkle to the implementation is that it needs to null out its internal pointer when the object gets collected.


> Also (for Jason, in particular) it occurs to me that this code:
> 
>    assert(wa.ptr !is null);
>    collect();
>    assert(wa.ptr is null);
> 
> Has to store wa.ptr somewhere (register or stack) when you ask to check 
> if it is null or not.  So that act of checking itself will likely make a 
> reference that prevents the following collect() from cleaning it up.

That's true that that might cause the last assert to fail, oops.  I don't know what the correct way to clear everything out is (including from the preceding function call).  Any more tips on making a more correct unit test are appreciated. 

Jarret was having issues with non-collection (ever) when using weak ref's and I had written this test to try and exercise it (his problem is windows + tango + dmd).  An assert failure doesn't mean it won't ever collect but will narrow the search down and allow closer examination... and the crash on linux+tango+gdc is definitely not correct behavior.



More information about the Digitalmars-d mailing list