D2 weak references

Sean Kelly sean at invisibleduck.org
Mon Apr 20 13:00:13 PDT 2009


== Quote from Jason House (jason.james.house at gmail.com)'s article
> Jason House Wrote:
> >
> > If I understand you correctly, a weak reference library needs to query the GC to see if an object is
marked for collection. How do I do this?
> Here's the implementation I'm currently thinking of:
> weak ref construction:
> Copy pointer into struct without casting
> Call rt_attach_dispose_event
> weak ref dereference:
> copy value into temp var
> ** query gc to see if garbage **
> If not garbage, Recopy value and return
> If garbage set value = null
> dispose of weakly referenced obj:
> set value = null
> The emphasized line is impossible according to the publicized gc interface. Given the delayed
finalization, I don't see any way around it.

You're guaranteed that the GC will notify you when it finalizes
the referenced object, so you shouldn't have to query the GC
at all.  Just copy the reference into a GC-scannable location
and then if you haven't been notified by the GC that the object
was finalized the reference is good.



More information about the Digitalmars-d mailing list