weak references

Bill Baxter wbaxter at gmail.com
Wed Aug 6 07:52:51 PDT 2008


They're good for signaling setups for one.  For instance, std.signals
does not use weak references.  That means if you connect object A's
signal to B's method, B cannot be collected ever.  std.signal uses the
GC's callback-on-delete mechanism so it can clean up properly if B is
/explicitly/ deleted by someone, but B will not be collected by the GC
itself because of the reference A holds.  For many cases there, you
would prefer A's signal to only have a weak reference to B, so that B
could be collected.

--bb

On Wed, Aug 6, 2008 at 11:26 PM, dsimcha <dsimcha at yahoo.com> wrote:
> == Quote from PJP (pete.poulos at gmail.com)'s article
>> Does the D garbage collector provide something similar to the "weak" references
> in Java?  Weak references don't prevent the object they refer to from being
> garbage collected.  If the object has been garbage collected, the weak reference
> will return null.
>
> Stupid question:  Why would you want weak references other than for circular
> reference elimination with a reference counting GC?
>


More information about the Digitalmars-d-learn mailing list