[vwnc] Finalization

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[vwnc] Finalization

Rusty-24
Hi.
I found myself in a situation where I need to release external resources.
In (some other Smalltalk) I use to do something like:

MyClass new
  onFinalizeDo: #aSelector;
  yourself.

If I underestand the documentation correctly, in VW I need to use WeakDictionary ... or Ephemerons.

What if I don't need to keep a count of my finalizable objects ?
I just need a method executed (or a block evaluated) when the object gets GC'd.

Is there any way of doing so without using additional resources ?
Or should I go ahead and create a WeakDictionary by hashs in a global or something and add each object on instanciation ?

Thanks.

Rusty.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Finalization

Travis Griggs-3

On Jun 26, 2008, at 1:46 AM, Rusty wrote:

> Hi.
> I found myself in a situation where I need to release external  
> resources.
> In (some other Smalltalk) I use to do something like:
>
> MyClass new
>   onFinalizeDo: #aSelector;
>   yourself.
>
> If I underestand the documentation correctly, in VW I need to use  
> WeakDictionary ... or Ephemerons.
>
> What if I don't need to keep a count of my finalizable objects ?
> I just need a method executed (or a block evaluated) when the object  
> gets GC'd.
>
> Is there any way of doing so without using additional resources ?
> Or should I go ahead and create a WeakDictionary by hashs in a  
> global or something and add each object on instanciation ?


There is a general purpose solution to this... kind of. There's a  
package in the Open Repository called "Weaklings" that provides this  
kind of behavior.

If you load the latest trunk version (version without a dot in the  
name), then you can "wrap" your object with a "last rites" actions. So  
you can do:

instance := MyClass new.
weakRef := instance withLastRites: [:obj | obj doCleanupStuff]

You send value to weakRef to get your instance. This pattern works  
well when you want to hold an object, but hold it weakly and have some  
cleanup happen when you quit holding it.

It doesn't work well if you want to just attach arbitrary finalization  
actions to objects anonymously and not hold the weakRef alive some  
where. The latest branch version of Weaklings, done by Boris Popov  
provides a solution to this. I need to get integrate this to the  
trunk, there were some method name changes I wanted to make, but other  
than that, I think it should work fine for the anonymous case.

--
Travis Griggs
Objologist
"You A students, you'll be back soon teaching here with me. You B  
students, you'll actually go on to be real engineers. You C students,  
you'll go into management and tell the A and B students what to do." -  
My Fluid Dynamics Professor whom I have yet to disprove


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc