Hello,

In message "Re: Won't WIN32OLE_EVENT.new leak?"
    on 07/08/20, "nikolai.weibull / gmail.com" <nikolai.weibull / gmail.com> writes:

> > > Can't the WIN32OLE_EVENT object be linked to the WIN32OLE object so
> > > that when the WIN32OLE object is freed, we make sure WIN32OLE_EVENT is
> > > freed as well?
> >
> > Thank you. It seems to me that your suggestion is good solution.
> > I'd like to study this solution.
> > (I need some time to study.)
> 
> OK.  I'll see if I can come up with a patch as well.  I solved the
> problem for my project by doing everything in a separate process,
> which would have been easier if Windows supported fork(), but I guess
> one can't get everything one wants...

I noticed that backword compatibility would be lost if 
WIN32OLE_EVENT object is freed when WIN32OLE object is freed.

For example, the following script would not work.

def ie_start
  ie = WIN32OLE.new('InternetExplorer.Application')
  ie.visible = true
  ev = WIN32OLE_EVENT.new(ie, 'DWebBrowserEvents')
  ev.on_event("Quit") {|*args| $LOOP = false}
  ev.on_event("BeforeNavigate") {|*args| puts args[0]}
  ie = nil
  GC.start
end
$LOOP = true
while $LOOP
  WIN32OLE_EVENT.message_loop
end

In current Win32OLE, even if ie would be freed by GC.start,
ev is not freed. And InternetExplorer keep running.
You can control InternetExplorer interactively.
While you would keep navigating any web page,
ev keep recieving the events from InternetExplorer.

As your suggestion, if ev would be freed when ie is freed,
then there is no way to recieve the events
from InternetExplorer.

So I could find the way how to free ev 
when InternetExplorer process is terminated,
but I have not found it.

  Regards,
  Masaki Suketa