# Ending Application When Window Closes

**URL:** <https://www.ruby-forum.com/t/ending-application-when-window-closes/183463>\
**Category:** wxRuby\
**Created:** [February 15, 2010, 2:24am UTC](https://www.ruby-forum.com/t/ending-application-when-window-closes/183463 "2010-02-15T02:24:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 15, 2010, 2:24am UTC](https://www.ruby-forum.com/t/ending-application-when-window-closes/183463/1 "2010-02-15T02:24:05Z")

</div>

OK, I _KNOW_ I’ve got a case of tunnel vision here. I’ve written apps  
that use wxRuby without a problem. Regardless, I don’t see what I’m  
doing wrong here. The code in my main routine looks like this:

class MyApp \< Wx::App  
def on\_init  
f = Video.new(nil)  
f.show  
end  
end

app = MyApp.new  
app.main\_loop

There is only one simple window in the app - created via wxFormBuilder.  
The window (a frame) displays cleanly. Problem: when I close this window  
(either by clicking the “X” or by issuing a call to self.close() through  
an event, the app doesn’t exit. It simply goes to sleep. The only way I  
see to close it is to go to a terminal and issue a “kill” command.

I’m running under Ubuntu 9.10.

Would someone be so kind as to point out what I’m missing - which I’m  
sure is going to be obvious to everyone but me.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 8, 2010, 2:43pm UTC](https://www.ruby-forum.com/t/ending-application-when-window-closes/183463/2 "2010-03-08T14:43:17Z")

</div>

I’ve had similar behavior and it was due to the app still holding  
resources. For me, I created a dock\_icon that wants to be destroyed  
prior to exiting.

So, in short, check if your code still holds any resources that need  
manual destruction and call them in an on\_close method … for instance:

class YourFrame \< Wx::Frame  
def initialize  
(…)

evt\_close :on\_close

(…)

@dock\_icon = Wx::TaskBarIcon.new  
@dock\_icon.set\_icon((Wx::Icon.new(“icon.png”,  
Wx::BITMAP\_TYPE\_PNG)),“Your App”)  
end

(…)

def on\_close  
@dock\_icon.destroy if @dock\_icon  
destroy  
end  
end

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 8, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/ending-application-when-window-closes/183463/3 "2010-03-08T14:46:50Z")

</div>

Tony M. wrote:

> I’ve had similar behavior and it was due to the app still holding  
> resources. For me, I created a dock\_icon that wants to be destroyed  
> prior to exiting.  
> Thanks Tony. Yes, this normally. As far as know, the only things that  
> need manual destruction are:

TaskBarIcon  
Dialog, when created with no window as parent

What these have in common is that they are GUI elements which should  
persist even if there is no main window (some kind of Wx::Frame) for the  
application. Everything else should be cleaned up automatically as an  
application runs and windows open and close.

a
