How to run code when an object is GC-ed

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

How to run code when an object is GC-ed

Sean P. DeNigris
Administrator
I have objects that are proxies for Ruby objects, and I want the ruby side to be alerted when the proxies are gc-ed so I can remove them.  How do I hook to run the code when the object is garbage collected?

Googled: "squeak garbage collection hook" and "squeak run code when object is garbage collected", still clueless :)

Thanks.
Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: How to run code when an object is GC-ed

Levente Uzonyi-2
On Fri, 29 Oct 2010, Sean P. DeNigris wrote:

>
> I have objects that are proxies for Ruby objects, and I want the ruby side to
> be alerted when the proxies are gc-ed so I can remove them.  How do I hook
> to run the code when the object is garbage collected?
>
> Googled: "squeak garbage collection hook" and "squeak run code when object
> is garbage collected", still clueless :)

It's called finalization. The easiest way to do it is sending
#toFinalizeSend:to:with: to your proxy. Something like this should work,
though your method names and objects are probably different:

proxy toFinalizeSend: #removeRubyObject: to: rubyConnection with: proxy rubyId.

If you need more advanced features, then check out WeakRegistry and
the following methods (in Object): #actAsExecutor, #finalize. Socket and
StandardFileStream uses this mechanism to deallocate external resources
in the image. Check out their methods in the registry and finalization
categories (on both instance and class side).


Levente

>
> Thanks.
> Sean
> --
> View this message in context: http://forum.world.st/How-to-run-code-when-an-object-is-GC-ed-tp3019930p3019930.html
> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: How to run code when an object is GC-ed

Sean P. DeNigris
Administrator
Levente Uzonyi-2 wrote
It's called finalization... proxy toFinalizeSend: #removeRubyObject: to: rubyConnection with: proxy rubyId.
Perfect, thanks :)

Sean
Cheers,
Sean