From: Eric Hodel Date: 2005-04-13T16:43:48+09:00 Subject: Re: ability to run finalizers at a given point of a program? --Apple-Mail-39--220468687 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On 13 Apr 2005, at 00:28, Guillaume Cottenceau wrote: >> So, I am getting the same result ts was getting. > > Now that is strange.. :/ > >>> What difference do you pretend your program is making with mine? >> >> The difference is in this: >> >> (your version): >> def scopeme >> foo = Foo.new >> ObjectSpace.define_finalizer(foo, proc { puts "*** >> pseudo-destructor" }) >> end >> >> That block captures foo as well. As a result, that instance will never >> be unreferenced, and thus never going to be GC'ed until the program >> ends. >> >> OTOH, ts' version: >> class Foo >> def self.final >> proc { puts "*** pseudo-destructor" } >> end >> end >> >> def scopeme >> foo = Foo.new >> ObjectSpace.define_finalizer(foo, Foo.final) >> end >> >> Notice that the finalizer block does not capture the Foo instance. > > Wait.. both calls to define_finalizer have a reference to foo as the > first argument, and both have a closure as second argument which > doesn't have a reference to foo. Incorrect. The first argument is irrelevant. ObjectSpace::define_finalizer is written this way probably to make things ugly. You have to pass in the object you wish to attach a finalizer to, and that's what the first arg is for. In your version, foo is in-scope when the finalizer proc is created, so the proc captures foo *even though it is not explicitly used in the proc*. That's right! Just because you don't reference foo inside a closure doesn't mean it magically disappears for you. -- Eric Hodel - drbrain@segment7.net - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --Apple-Mail-39--220468687 content-type: application/pgp-signature; x-mac-type=70674453; name=PGP.sig content-description: This is a digitally signed message part content-disposition: inline; filename=PGP.sig content-transfer-encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iEYEARECAAYFAkJczZkACgkQMypVHHlsnwQCegCgkVahNV1GLJLZY239fQ6+Gp5K y5cAni9wxFq/d48/VoporyDssfv83csz =t2XY -----END PGP SIGNATURE----- --Apple-Mail-39--220468687--