Weak references.

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Apr 12 17:09:21 PDT 2008


"Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
news:ftr9as$12s2$1 at digitalmars.com...
> I've been looking at some of my old nonagon code, and as I'm sure you've 
> all experienced looking at your old code, I'm just _cringing_ at some of 
> the things I did in it ;)
>
> One thing that I've noticed would be nice to have, however, would be weak 
> references to some objects.  For example, I have "brush" objects which 
> encapsulate a bunch of visual settings (textures, material, blending modes 
> etc.).  These are referenced by objects that can be rendered.  Some of 
> these brushes need to be updated often (i.e. every frame to update the 
> environment mapping matrices).  So I keep a list (table) of all instances 
> of this brush class so I can easily loop through them and update them all.
>
> However when their owners disappear, I end up with uncollectable brushes, 
> since they are still being referenced by that table.
>
> So what would be nice to have happen would be to have that table be a 
> table of _weak references_ to brush objects.  Then, when no more real 
> references existed, they could be collected, and I wouldn't be keeping 
> them around for no reason anymore.
>
> I've looked into the scrapple weakref implementation, but it doesn't seem 
> to do what I want.  Seemingly it only works if the object that it 
> references is explicitly deleted.  I want the weakref to become null if 
> the object that it references is collected in any way.
>
> Is this possible at all?  Am I just not using Bill's weakref properly?

To both: yes, that's what I would have expected, but I can't seem to get the 
object to be collected even if the only reference to it is a weak reference. 
Tried forcing a full collect, allocating bunches of memory, collecting 
again, allocating at the beginning of a long-running program etc.  it never 
seems to get collected, even if I never assign the reference into an actual 
pointer type.

I'm using Tango if it makes any difference. 





More information about the Digitalmars-d mailing list