Monday, December 24, 2012

ObjectSpace::define_finalizer says no

In Ruby, one has to be extremely careful when using ObjectSpace::define_finalizer. I fell for this trap:

See the problem here? Well, I didn't at first, until I noticed that my program only freed memory at shutdown. The problem here is that the proc is created with the obj instance as part of it's scope... or self, even if it isn't used inside the proc, which is the infamous ObjectSpace::define_finalizer bug.

To correct, I removed the obj parameter and called define_finalizer directly like so:

This time around my computer started to say yes:

I hope this post may help someone, since I didn't find anything on the interwebs that exposed this specific issue.