Weak references.

Bill Baxter dnewsgroup at billbaxter.com
Sun Apr 13 21:00:18 PDT 2008


Sean Kelly wrote:
> == Quote from Jason House (jason.james.house at gmail.com)'s article
>> Sean Kelly wrote:
>>> == Quote from Jarrett Billingsley (kb3ctd2 at yahoo.com)'s article
>>>> "Sean Kelly" <sean at invisibleduck.org> wrote in message
>>>> news:fttd02$1mp1$1 at digitalmars.com...
>>>>> Are these tables AAs?  And how do the weak references store their
>>>>> reference?
>>>>> The type information provided to AAs is a bit weak, and so some
>>>>> non-pointer
>>>>> data is currently still scanned by the GC.  I'm going to improve this a
>>>>> bit in the
>>>>> next Tango release, but it still won't be perfect (because the compiler
>>>>> doesn't
>>>>> provide enough info for it to be).
>>>> In my tests I've just been doing
>>>> auto weakref = new WeakRef!(A)(new A);
>>>> and then checking that after doing god-knows-what to try to get it to
>>>> collect that instance of A, so it's not even that I'm doing anything that
>>>> crazy.
>>> Just for kicks, try querying the GC to determine whether the GC thinks
>>> weakref contains pointers.
>>>
>>>
>>> Sean
>> How would one do that?
> 
> (from memory)
> 
>     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.


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.

--bb



More information about the Digitalmars-d mailing list