www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - weak references

reply PJP <pete.poulos gmail.com> writes:
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.
Aug 05 2008
next sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
It provides a call-back hook that can be set to get a notification
when an object you're interested in is destroyed.
And in general size_t's and other integer types are not treated as pointers.

These two things can be used to make a weak ref class, which is what
has been done here:

http://www.dsource.org/projects/scrapple/browser/trunk/weakref

On Wed, Aug 6, 2008 at 2:06 PM, PJP <pete.poulos gmail.com> wrote:
 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.
Aug 05 2008
parent reply PJP <pete.poulos gmail.com> writes:
I don't think this solution is safe.  According to
(http://www.digitalmars.com/d/2.0/garbage.html), this is undefined behavior,
"Do not store pointers into non-pointer variables using casts and other tricks.
"  and "Do not store into pointers values that may point into the garbage
collected heap...A copying garbage collector may change this value"

PJP

Bill Baxter Wrote:

 It provides a call-back hook that can be set to get a notification
 when an object you're interested in is destroyed.
 And in general size_t's and other integer types are not treated as pointers.
 
 These two things can be used to make a weak ref class, which is what
 has been done here:
 
 http://www.dsource.org/projects/scrapple/browser/trunk/weakref
 
 On Wed, Aug 6, 2008 at 2:06 PM, PJP <pete.poulos gmail.com> wrote:
 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.
Aug 06 2008
parent "Bill Baxter" <wbaxter gmail.com> writes:
On Wed, Aug 6, 2008 at 9:13 PM, PJP <pete.poulos gmail.com> wrote:
 I don't think this solution is safe.  According to
(http://www.digitalmars.com/d/2.0/garbage.html), this is undefined behavior,
"Do not store pointers into non-pointer variables using casts and other tricks.
"
This one is I think just trying to warn you that your pointer stored in a non-pointer variable may likely be collected out from underneath you. But that's exactly what you want out of a weak reference.
 and "Do not store into pointers values that may point into the garbage
collected heap...A copying garbage collector may change this value"
That one is more troublesome, but a) D is not likely to get a copying collector any time soon. b) using a WeakRef class now for all your weak reference needs should make it easy to upgrade all your code. Just replace WeakRef's implementation with the built in thing. c) It's all there is for now, as far as I know. d) std.signals relies on the same trick. That said, I kind of wrote it just because it seemed like it should work, and it was needed. But I wasn't really sure if it would work (or even if it works properly now -- it's quite hard to test because the GC has no diagnostics to tell you about what outstanding references it thinks exist). So I was half expecting someone to say "no no no that's not how you write a weak ref in D, /here's/ how you do it", and then the plan was that I would use that one too. :-) But no such person stepped out of the woodwork. --bb
 PJP

 Bill Baxter Wrote:

 It provides a call-back hook that can be set to get a notification
 when an object you're interested in is destroyed.
 And in general size_t's and other integer types are not treated as pointers.

 These two things can be used to make a weak ref class, which is what
 has been done here:

 http://www.dsource.org/projects/scrapple/browser/trunk/weakref

 On Wed, Aug 6, 2008 at 2:06 PM, PJP <pete.poulos gmail.com> wrote:
 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.
Aug 06 2008
prev sibling parent reply dsimcha <dsimcha yahoo.com> writes:
== Quote from PJP (pete.poulos 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?
Aug 06 2008
next sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
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 yahoo.com> wrote:
 == Quote from PJP (pete.poulos 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?
Aug 06 2008
prev sibling next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"dsimcha" <dsimcha yahoo.com> wrote in message 
news:g7ccbe$2b00$1 digitalmars.com...
 == Quote from PJP (pete.poulos 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?
I'd like them to be able to keep track of all instances of a class in existence without actually holding true references to them. That being said, I've never been able to get Bill's weakref.d to work properly. I've filed a Tango bug report but Sean can't reproduce it while I can.
Aug 06 2008
prev sibling parent PJP <pete.poulos gmail.com> writes:
 Stupid question:  Why would you want weak references other than for circular
 reference elimination with a reference counting GC?
Here are three examples where weak references may or may not be useful: 1) Suppose your application uses a set of large resources (Graphics resources, for instance) and you want to implement a cache for these values such that any request for the same resource returns a shared instance to that resource (to keep the memory footprint down). However, when the resource is no longer in use, you would like it to be garbage collected. If your cache maintains a reference to the resource, which it must, then it will prevent that resource from being reclaimed which results in a memory leak. With weak references, you have the cache maintain weak references to the resources and if the cache is the only thing with a refence to the resource it can be reclaimed by the garbage collector. You can solve this problem without weak references, in a variety of ways (reference counting or using a fixed cache size and a cache miss algorithm like LRU), and in some cases they may be better solutions for performance reasons. However, they also require more overhead and and are more error prone (especially if they require the user to release the references to the cache when they are done with them). 2) Suppose you have a class, Window, which provides the operation "minimizeAllWindows." To implement this, your class needs to maintain a set of all instances of itself. However, doing so would prevent each of those items from being garbage collected. With weak set (built using weak references) this set wouldn't prevent the individual instances from being garbage collected. 3) Suppose you have an object, NewsBroadcast, which delivers news to all subscribers. The NewsBroadcast object must maintain a reference to all subscribers which will in turn hold all of those subscribers in memory. However, suppose that your subscriber would like to be reclaimed when nothing but the NewsBroadcast holds a reference to it. The only way to do this without weak references is to implement a form of reference counting on the subscriber so that you know when NewsBroadcast is the only object with a reference to the subscriber. When this condition occurs you then unsubscribe from the NewsBroadcast and the object can be reclaimed. However, this solution is inelegant and very error prone. PJP
Aug 06 2008