# ObjectSpace.define\_finalizer does not run some procedure str

**URL:** <https://www.ruby-forum.com/t/objectspace-define-finalizer-does-not-run-some-procedure-str/82181>\
**Category:** Ruby\
**Created:** [January 17, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/objectspace-define-finalizer-does-not-run-some-procedure-str/82181 "2007-01-17T22:06:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 17, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/objectspace-define-finalizer-does-not-run-some-procedure-str/82181/1 "2007-01-17T22:06:37Z")

</div>

I have Ruby 182-15 on WinXP

When I run the code below only the first finalizer runs. The others  
two don’t seem to run. I found similar posts on other sites, but no  
solutions.

You can duplicate the first and it runs twice (so two finalizers is  
ok).

To me they are all somewhat identical. Can anyone tell me why it does  
not work.

I have a number of machines that run testware in ruby 152 so I’d like  
to KNOW that upgrading will fix the problem before taking this path.

Paul

------- Test.rb --------

class MyClass

```
def initialize

```

ObjectSpace.define\_finalizer(self,self.class.method(:finalize).to\_proc)  
ObjectSpace.define\_finalizer(self,proc{|id| puts “proc final  
#{id}”})  
ObjectSpace.define\_finalizer(self,self.dofinalize)  
end

```
def self.finalize(id)
    puts "Object #{id} dying at #{Time.new}"
end

def self.f2(id)
    puts "F2 #{id}"
end

def dofinalize
  return Proc.new { |param|
    puts "Doing finalize #{param[0]}"
  }
end

```

end

MyClass.new  
ObjectSpace.garbage\_collect

puts “DONE”

-------- output -------

> Test.rb  
> DONE  
> Object 20691624 dying at Thu Jan 18 09:55:38 New Zealand Standard Time  
> 2007

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 17, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/objectspace-define-finalizer-does-not-run-some-procedure-str/82181/2 "2007-01-17T22:56:46Z")

</div>

On 17.01.2007 22:00, removed\_email\_address@domain.invalid wrote:

> not work.
> 
> ```
> end
> 
> ```
> 
> end  
> DONE  
> Object 20691624 dying at Thu Jan 18 09:55:38 New Zealand Standard Time  
> 2007

There’s a subtle difference between the first and the other two: the  
first one does not create a closure which binds “self” to the current  
instance. That might be an explanation.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 17, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/objectspace-define-finalizer-does-not-run-some-procedure-str/82181/3 "2007-01-17T23:22:52Z")

</div>

On Thu, 18 Jan 2007 removed\_email\_address@domain.invalid wrote:

> not work.  
> finalizers cannot refer the to object being finalized. in the second  
> and  
> third cases you hold a reference to the object (implicit in  
> Proc.new/proc)  
> inside the created closures - thereby preventing them from being run.  
> inotherwords you’ve created finalizers that prevent the object from  
> being  
> finalized! 😉

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 18, 2007, 10:40am UTC](https://www.ruby-forum.com/t/objectspace-define-finalizer-does-not-run-some-procedure-str/82181/4 "2007-01-18T10:40:59Z")

</div>

On 17.01.2007 23:22, removed\_email\_address@domain.invalid wrote:

> > To me they are all somewhat identical. Can anyone tell me why it does  
> > not work.
> 
> finalizers cannot refer the to object being finalized. in the second and  
> third cases you hold a reference to the object (implicit in Proc.new/proc)  
> inside the created closures - thereby preventing them from being run.  
> inotherwords you’ve created finalizers that prevent the object from being  
> finalized! 😉

Still the fact remains interesting that at least one of the other  
finalizers is being called when the process exits. So the “blocking”  
finalizers just block themselves but not other finalizers.

10:35:25 [Temp]: ruby fin.rb  
test4 136802980 2  
test4 136802980 3  
test3 136803090 1  
test3 136803090 2  
test2 136803180 1  
test2 136803180 2  
test1 136803250 1  
test1 136803250 2  
10:36:20 [Temp]: cat fin.rb

def test1  
o = Object.new  
ObjectSpace.define\_finalizer(o) {|id| puts “test1 #{id} 1”}  
ObjectSpace.define\_finalizer(o) {|id| puts “test1 #{id} 2”}  
end

def test2  
o = Object.new  
ObjectSpace.define\_finalizer(o) {|id| puts “test2 #{id} 1”}  
ObjectSpace.define\_finalizer(o) {|id| puts “test2 #{id} 2”}  
ObjectSpace.define\_finalizer(o) {|id| puts “test2 #{id} 3  
#{o.inspect}”}  
end

def test3  
o = Object.new  
ObjectSpace.define\_finalizer(o) {|id| puts “test3 #{id} 1”}  
ObjectSpace.define\_finalizer(o) {|id| puts “test3 #{id} 2”}

o.instance\_eval do  
ObjectSpace.define\_finalizer(o) {|id| puts “test3 #{id} 3  
#{inspect}”}  
end  
end

def test4  
o = Object.new  
o.instance\_eval do  
ObjectSpace.define\_finalizer(o) {|id| puts “test4 #{id} 1  
#{inspect}”}  
end

ObjectSpace.define\_finalizer(o) {|id| puts “test4 #{id} 2”}  
ObjectSpace.define\_finalizer(o) {|id| puts “test4 #{id} 3”}  
end

test1  
test2  
test3  
test4  
10:36:27 [Temp]:

Cheers

robert
