From: djlewis Date: 2008-08-13T08:52:36+09:00 Subject: Re: WIN32OLE -- cannot terminate Excel process On Aug 12, 6:31 pm, Adam Shelly wrote: > On 8/12/08, David Mullet wrote: > > > If I modified that code to this... > > > ews = nil > > ewb = nil > > excel = nil > > WIN32OLE.ole_free( ews) > > WIN32OLE.ole_free( ewb) > > WIN32OLE.ole_free( excel) > > GC.start > > > ...the Excel process terminated properly. > > > My suggestions: > > > A) Set object variables to nil before calling ole_free. > > I can see how you can free up object memory with > ews = nil; > #... > GC.start > > But I'm not sure what calling ole_free after assigning to nil could > possibly do... > How is > ews = nil; > WIN32OLE.ole_free( ews) > > any different from: > WIN32OLE.ole_free( nil) > ? > > -Adam Thanks for all the suggestions. One of my problems was due to running in a debugger (NetBeans) and stopping at a breakpoint to look at processes, and sometimes even just killing execution right there. That prevented the Excel process from terminating, perhaps because GC did not finish its job. Still, during debugging, that's a common occurrence, and can lead to a lot of Excel processes hanging around. So I think the function simply to kill all automation instances of Excel is handy to have while debugging, used, say, at the beginning of the program. Here it is... def kill_excel_automation_processes wmi = WIN32OLE.connect("winmgmts://") processes = wmi.ExecQuery("select * from win32_process where commandline like '%excel.exe\"% /automation %'") processes.each do |process| Process.kill('KILL', process.ProcessID) end end I don't know if there's a way to get something called upon manual termination in the debugger. at_exit { } does not seem to do it in NetBeans.