Weak references.
Sean Kelly
sean at invisibleduck.org
Mon Apr 14 10:35:16 PDT 2008
== Quote from Jason House (jason.james.house at gmail.com)'s article
> 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.
The easiest thing I can think of would be to do an access of a dummy weakptr
after each access of the real weakptr:
auto dummy = new WeakPtr!(void);
assert(wa.ptr !is null);
assert(dummy.ptr is null);
collect();
assert(wa.ptr is null);
Since the dummy op is identical to the real op, it should hopefully hit the same
registers. Best way to be sure would be to test with -O turned off.
> 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.
Thanks for any investigation you do for this. If you can find a bug in Tango, I'd be
happy to fix it, but I'm too busy to do the research myself at the moment.
Sean
More information about the Digitalmars-d
mailing list