Masaki Suketa schrieb:
I am not sure, but it seems to me because of GC behavior.
I tested your script using Foo class instead of WIN32OLE.
And I've got same result as WIN32OLE.
I cannot confirm that.
The last two outputs give the empty list on my system.
Independent of the Ruby version (1.8.6 or 1.8.5).
--- code starts ---
class Foo
def initialize(arg)
@arg = arg
end
def Quit
end
end
def puts_win32ole_objects
res =
ObjectSpace.each_object do |o|
res << o if o.is_a? Foo
end
puts res.inspect
end
puts_win32ole_objects # --> empty
xl = Foo.new('Excel.Application')
puts_win32ole_objects # --> one object
xl.Quit
xl = nil
GC.start # anything else I could do???
puts_win32ole_objects # --> the object is still there
sleep 5
puts_win32ole_objects # --> the object is still there --- code end ---
The Excel process is terminated when the WIN32OLE object is GCed.
But in this case, the WIN32OLE object is not GCed, so the Excel process is not terminated.
I see: it is all a matter of garbage collection.
But if you want to terminate the process before the WIN32OLE object GCed, you can use WIN32OLE#ole_free.
Thanks for telling me -- this worked indeed, contrary to WIN32OLE.ole_free.
It would be a dirty solution.
However, The point you made about GC, has inspired me towards further
experiments.
First, I created a second WIN32OLE.new('Excel Application'), just to see
if the first object might get 'recycled'.
What I found was, that two objects existed after the second object creation,
but after the three terminating statements (quit -- nil -- GC),
only the second object remained. (Lazy recycling, ...
)
Encouraged by this surprising result, I tried other things, and then this:
xl = Foo.new('Excel.Application')
puts_win32ole_objects
xl = nil
GC.start
Just not calling xl.Quit anymore, and -- the object is gone!
Simplest of all!
This looks like an easy solution, but no no, too early to jump for joy:
I encountered this behaviour for _any_ method I called on the xl object.
OK, so, the easy destruction only works for "virgin" objects.
Thus, so far, we have not yet found a gentle
way to finish a WIN32OLE/Excel object.
For the time being, I think I'll resort to
(not-so-gentle) WIN32OLE#ole_free.
Regards,
Sven