Watching the discussion on the Ruby Rogues Parley about the difference between procs and lambdas I realized that, by being tightly coupled with the context they are created, procs could easily create memory leaks.
The following example illustrates that:
As you can see, because the Printer class caches every block for future use, not one LeakyFoo has been collected by the GC.
Unfortunately is extremely easy to forget about this behavior, so anyone (even me) is likely to write the following piece of code to define an instance finalizer:
One solution to this problem is to create the Proc object in a different context:
Sadly, this is the kind of behavior that can bring subtle and catastrophic bugs.