Problem closing window with Close(); Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Problem closing window with Close();

Post by Samuel-FES » Thu May 03, 2012 9:18 pm

Hi all:

So I an new to wxWidget. I was just trying to run the first default sample code right after creating a new project, (where the wxSmith tutorial tells you to add the Close();) in the OnClose event. I could not close the window as I get the popup - watever.exe stops working, and I have the message "Process terminated with status -1073741571 (0 minutes, 5 seconds)"

Any ideas?

I can close window fine with event.Skip(true) and Destroy();

Thanks!

wxWidget 2.8.12, Complier GNU GCC Compiler, Code:Block 10.05, wxSmith

computer is windows 7 x64

Sam

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: Problem closing window with Close();

Post by Auria » Fri May 04, 2012 1:27 am

When you have a crash, the first step is to run the application in a debugger to find out where it crashes
"Keyboard not detected. Press F1 to continue"
-- Windows

Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Re: Problem closing window with Close();

Post by Samuel-FES » Fri May 04, 2012 5:54 pm

Hi all:

Ah sorry for missing that essential info. Heres my debugger, hope I got the right info. So it stopped at Close();

Starting debugger:
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb 6.8
Child process PID: 1676
Program received signal SIGSEGV, Segmentation fault.
In wxWindowBase::Close () (C:\SourceCode\Libraries\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll)
At C:\.....\Full Energy Solution\Test\Test3\Test3Main.cpp:108

I uploaded my screenshot from call stack:

Please see after step in for what happened after I step into the Close();

I get this msg after stepping into Close();

In wxWindowBase::Close () (C:\SourceCode\Libraries\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll)
At C:\....\Full Energy Solution\Test\Test3\Test3Main.cpp:108
Program received signal SIGSEGV, Segmentation fault.
In ntdll!LdrQueryProcessModuleInformation () (C:\Windows\system32\ntdll.dll)

Thanks !

Sam
Attachments
After Step In.png
This is what happened after step into Close();

In wxWindowBase::Close () (C:\SourceCode\Libraries\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll)
At C:\Users\chiang ming-chu\Documents\Sam\Full Energy Solution\Test\Test3\Test3Main.cpp:108
Program received signal SIGSEGV, Segmentation fault.
In ntdll!LdrQueryProcessModuleInformation () (C:\Windows\system32\ntdll.dll)
Before Step In.png
Before I step into Close();

This is where I get the msg I posted on reply.

User avatar
doublemax
Moderator
Moderator
Posts: 14080
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem closing window with Close();

Post by doublemax » Fri May 04, 2012 6:37 pm

Calling Close() in an OnClose() handler leads to an infinite recursion, as you can see in the 2nd screenshot.

The default behavior of a wxFrame when it its closed, is to destroy itself. So usually you don't have to do anything.
Use the source, Luke!

Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Re: Problem closing window with Close();

Post by Samuel-FES » Fri May 04, 2012 6:50 pm

Ah! So for the OnClose...I can either use event.Skip(true); or Destroy(); then :) ?

:( hmm then why did the tutorial tell us to put the Close(); in OnClose() ??? this is from wxSmith Tutorial...it says if I dont put anything in OnClose the window will not close...

Thanks :D

Sam

User avatar
doublemax
Moderator
Moderator
Posts: 14080
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem closing window with Close();

Post by doublemax » Fri May 04, 2012 7:03 pm

So for the OnClose...I can either use event.Skip(true); or Destroy(); then
Yes.
hmm then why did the tutorial tell us to put the Close(); in OnClose()
No idea, i don't know which tutorial you're referring to. Maybe it's used in a different context, e.g. in a menu item event.

I general, i recommend to start with the samples that come with wxWidgets. They're less likely to contain bugs.
Use the source, Luke!

Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Re: Problem closing window with Close();

Post by Samuel-FES » Fri May 04, 2012 7:07 pm

Ah ok. :( Tho this is the sample code, I just created a wxwidget project and just pressed "built and run"..if I don't put anything in the OnClose...the window will not close if I press the X button or the file->quit tab.

The tutorial is this

http://wiki.codeblocks.org/index.php?ti ... ello_world

I basically havnt started modifying code yet.

Thanks :)

Sam

User avatar
doublemax
Moderator
Moderator
Posts: 14080
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem closing window with Close();

Post by doublemax » Fri May 04, 2012 7:33 pm

In the link you posted, the Close() is ok, as it's called from a button event handler. And i don't see any OnClose() handler there.

Maybe the program version you're using now, creates different minimal code. Could you post the complete code it created?
Use the source, Luke!

Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Re: Problem closing window with Close();

Post by Samuel-FES » Fri May 04, 2012 7:54 pm

Sure Doublemax!

/***************************************************************
* Name: Test3Main.cpp
* Purpose: Code for Application Frame
* Author: Samuel-FES ([email protected])
* Created: 2012-05-03
* Copyright: Samuel-FES ()
* License:
**************************************************************/

#include "Test3Main.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(Test3Frame)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);

if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}

return wxbuild;
}

//(*IdInit(Test3Frame)
const long Test3Frame::idMenuQuit = wxNewId();
const long Test3Frame::idMenuAbout = wxNewId();
const long Test3Frame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Test3Frame,wxFrame)
//(*EventTable(Test3Frame)
//*)
END_EVENT_TABLE()

Test3Frame::Test3Frame(wxWindow* parent,wxWindowID id)
{
//(*Initialize(Test3Frame)
wxMenuItem* MenuItem2;
wxMenuItem* MenuItem1;
wxMenu* Menu1;
wxMenuBar* MenuBar1;
wxMenu* Menu2;

Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
MenuBar1 = new wxMenuBar();
Menu1 = new wxMenu();
MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
Menu1->Append(MenuItem1);
MenuBar1->Append(Menu1, _("&File"));
Menu2 = new wxMenu();
MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
Menu2->Append(MenuItem2);
MenuBar1->Append(Menu2, _("Help"));
SetMenuBar(MenuBar1);
StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
int __wxStatusBarWidths_1[1] = { -1 };
int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
SetStatusBar(StatusBar1);

Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Test3Frame::OnQuit);
Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Test3Frame::OnAbout);
Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&Test3Frame::OnClose);
//*)
}

Test3Frame::~Test3Frame()
{
//(*Destroy(Test3Frame)
//*)
}

void Test3Frame::OnQuit(wxCommandEvent& event)
{
Close();
}

void Test3Frame::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}

void Test3Frame::OnClose(wxCloseEvent& event)
{

// this is where I am told to use Close();
}


Thank you!!!

Sam

User avatar
doublemax
Moderator
Moderator
Posts: 14080
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem closing window with Close();

Post by doublemax » Fri May 04, 2012 8:09 pm

This can't work. If you catch the wxEVT_CLOSE_WINDOW event, you either have to call event.Skip() (so that the default handing = closing the frame, takes place) or Detroy() inside.

If the handler is empty, the frame won't close.
Use the source, Luke!

Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Re: Problem closing window with Close();

Post by Samuel-FES » Fri May 04, 2012 9:22 pm

Ah, that make sense

So to summarize:

in OnClose

event.Skip(true); means to let he OnClose do its job of destroying the window by default

Destroy(); adding this basically means to really make sure it destroys the window (redundant then I guess? more like just to make sure window always gets destroyed)

if we add close() that basically put the OnClose in a recursive loop.

Quick question then is when do we use Destroy() over event.Skip(true);...in your experience.

Thanks :D

Sam

User avatar
doublemax
Moderator
Moderator
Posts: 14080
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem closing window with Close();

Post by doublemax » Sat May 05, 2012 12:07 am

Quick question then is when do we use Destroy() over event.Skip(true);...in your experience.
Doesn't really matter, i personally would prefer event.Skip().

But you shouldn't call both at the same time.
Use the source, Luke!

Samuel-FES
Knows some wx things
Knows some wx things
Posts: 35
Joined: Thu May 03, 2012 9:01 pm

Re: Problem closing window with Close();

Post by Samuel-FES » Sat May 05, 2012 12:16 am

Thank you for the help Doublemax :D

Sam

Post Reply