# Won't WIN32OLE\_EVENT.new leak?

**URL:** <https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219>\
**Category:** Ruby\
**Created:** [August 16, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219 "2007-08-16T14:41:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 16, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219/1 "2007-08-16T14:41:58Z")

</div>

Won’t WIN32OLE\_EVENT.new leak? A WIN32OLE object will sooner or later  
be freed (right?), when garbage collection kicks in. But a  
WIN32OLE\_EVENT object will never be freed, as it is kept in a global  
array and is never removed from that array.

nikolai

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 17, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219/2 "2007-08-17T15:37:51Z")

</div>

Hello,  
In message “Won’t WIN32OLE\_EVENT.new leak?”  
on 07/08/16, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

> Won’t WIN32OLE\_EVENT.new leak? A WIN32OLE object will sooner or later  
> be freed (right?), when garbage collection kicks in. But a  
> WIN32OLE\_EVENT object will never be freed, as it is kept in a global  
> array and is never removed from that array.

WIN32OLE\_EVENT object is freed when ruby process terminated.  
And while ruby process running WIN32OLE\_EVENT object is not freed.

Because WIN32OLE\_EVENT object should not be freed until  
WIN32OLE object is freed.  
If WIN32OLE\_EVENT object is freed before WIN32OLE object is freed,  
then ruby is segmentation fault.  
To avoid this segmentation fault, WIN32OLE\_EVENT object is added  
in a global array and should not be freed.  
I think this solution is not so good, but I have not found  
the best (or better) solution.

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219/3 "2007-08-20T13:04:27Z")

</div>

Hello,  
In message “Re: Won’t WIN32OLE\_EVENT.new leak?”  
on 07/08/20, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.)

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2007, 9:10am UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219/4 "2007-08-20T09:10:52Z")

</div>

On Aug 17, 3:35 pm, Masaki S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And while ruby process running WIN32OLE\_EVENT object is not freed.
> 
> Because WIN32OLE\_EVENT object should not be freed until  
> WIN32OLE object is freed.

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?

nikolai

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 24, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219/5 "2007-08-24T14:23:48Z")

</div>

Hello,

In message “Re: Won’t WIN32OLE\_EVENT.new leak?”  
on 07/08/20, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

> 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 S.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/wont-win32ole-event-new-leak/112219/6 "2007-08-20T16:36:20Z")

</div>

On Aug 20, 1:02 pm, Masaki S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > And while ruby process running WIN32OLE\_EVENT object is not freed.  
> > > (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…

nikolai
