[squeak-dev] How to run code when an object is GC-ed

Levente Uzonyi leves at elte.hu
Sat Oct 30 01:53:56 UTC 2010


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



More information about the Squeak-dev mailing list