Jump to page: 1 2 3
Thread overview
Weak references.
Apr 12, 2008
Jason House
Apr 12, 2008
Bill Baxter
Apr 13, 2008
Frits van Bommel
Apr 13, 2008
Sean Kelly
Apr 14, 2008
Jason House
Apr 14, 2008
Bill Baxter
Apr 14, 2008
Jason House
Apr 14, 2008
Sean Kelly
Apr 14, 2008
Jason House
Apr 14, 2008
Sean Kelly
Apr 14, 2008
Bill Baxter
Apr 14, 2008
Jason House
Apr 14, 2008
Sean Kelly
Apr 14, 2008
Jason House
Apr 14, 2008
Lars Ivar Igesund
Apr 14, 2008
Sean Kelly
Apr 14, 2008
Sean Kelly
Apr 13, 2008
Michel Fortin
Apr 13, 2008
Bill Baxter
Apr 13, 2008
Robert Fraser
Re: Weak references. B.B; Keinfarbton
Apr 23, 2008
Bjoern
Apr 23, 2008
Tom S
April 12, 2008
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?


April 12, 2008
Take another look... Weak references are not viewed as references. This means the objects will get collected when only weak references exist. It sounds like that is what you want...

Jarrett Billingsley Wrote:

> 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?
> 
> 

April 12, 2008
Jarrett Billingsley wrote:
> 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? 
> 
> 

It does exactly what you're asking for ... as long as your "Brushes" are class instances, which it sounds like they are.
Instead of filling the table with type Brush, fill it with type WeakRef!(Brush).  Then when you go to use them from the table you should check for null first.  If the ptr value is null, it means the object got collected at some point, so you can remove it from the table.

--bb
April 13, 2008
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:ftr9as$12s2$1@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.


April 13, 2008
Jarrett Billingsley wrote:
> 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. 

Have you tried looking at the generated assembler code? Perhaps the compiler is leaving a reference in some register that's never overwritten afterwards or something...
April 13, 2008
On 2008-04-12 17:24:44 -0400, "Jarrett Billingsley" <kb3ctd2@yahoo.com> said:

> Is this possible at all?  Am I just not using Bill's weakref properly?

I've been using an alternate strategy in the D/Objective-C bridge for the bridged object lookup table. Basically, I'm casting object references to size_t (so they're no longer pointers) and mangling the pointers (so the garbage collector can't see them as pointers, I think this is no longer be necessary). When the bridged object gets deleted on the D side, it removes its mangled pointer from the table.

So if your referenced object knows about its weak reference in the table, you could implement it as such and don't have to worry about null pointers polluting your table.

-- 
Michel Fortin
michel.fortin@michelf.com
http://michelf.com/

April 13, 2008
== Quote from Jarrett Billingsley (kb3ctd2@yahoo.com)'s article
> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:ftr9as$12s2$1@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.

Are these tables AAs?  And how do the weak references store their reference?
The type information provided to AAs is a bit weak, and so some non-pointer
data is currently still scanned by the GC.  I'm going to improve this a bit in the
next Tango release, but it still won't be perfect (because the compiler doesn't
provide enough info for it to be).


Sean
April 13, 2008
"Frits van Bommel" <fvbommel@REMwOVExCAPSs.nl> wrote in message news:ftsq01$f05$1@digitalmars.com...
> Jarrett Billingsley wrote:
>> 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.
>
> Have you tried looking at the generated assembler code? Perhaps the compiler is leaving a reference in some register that's never overwritten afterwards or something...

That's what I was thinking, which is why I tried running lots of code after the allocation, to clear out the registers.

I've checked the memory block allocated for the WeakRef instance and it's marked as "finalize" and "no scan", which are both right.

I absolutely cannot get it to collect the instance that it references.

I really wonder if this is a bug in the Tango GC.


April 13, 2008
"Sean Kelly" <sean@invisibleduck.org> wrote in message news:fttd02$1mp1$1@digitalmars.com...
>
> Are these tables AAs?  And how do the weak references store their
> reference?
> The type information provided to AAs is a bit weak, and so some
> non-pointer
> data is currently still scanned by the GC.  I'm going to improve this a
> bit in the
> next Tango release, but it still won't be perfect (because the compiler
> doesn't
> provide enough info for it to be).

In my tests I've just been doing

auto weakref = new WeakRef!(A)(new A);

and then checking that after doing god-knows-what to try to get it to collect that instance of A, so it's not even that I'm doing anything that crazy.


April 13, 2008
Michel Fortin wrote:
> On 2008-04-12 17:24:44 -0400, "Jarrett Billingsley" <kb3ctd2@yahoo.com> said:
> 
>> Is this possible at all?  Am I just not using Bill's weakref properly?
> 
> I've been using an alternate strategy in the D/Objective-C bridge for the bridged object lookup table. Basically, I'm casting object references to size_t (so they're no longer pointers) 

Yes, that's what the weak ref class does.

> and mangling the pointers (so the garbage collector can't see them as pointers, I think this is no longer be necessary). 

I agree that that's not necessary.

> When the bridged object gets deleted on the D side, it removes its mangled pointer from the table.
> 
> So if your referenced object knows about its weak reference in the table, you could implement it as such and don't have to worry about null pointers polluting your table.

Good idea.  I was thinking to suggest something like that too.  A WeakRef class is not really an optimal way to keep track of a collection of weak refs.  In addition to null pointers polluting the table, there's also just the basic overhead of needing to create an extra object for each entry.  Ideally there would be WeakArray, WeakSet, WeakHeap... etc.  I think I've seen such things in some language.  Python maybe?

--bb
« First   ‹ Prev
1 2 3