Author Topic: wxMSW Bug in default Dialog source code made with Wizard  (Read 6671 times)

stefanos_

  • Guest
wxMSW Bug in default Dialog source code made with Wizard
« on: July 18, 2010, 12:21:40 am »
I have created a Dialog project with the help of wizard and without modifying it, the default code in wxDialogDefaultApp.cpp is

Code
bool wxDialogDefaultApp::OnInit()
{
    //(*AppInitialize
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
     wxDialogDefaultDialog Dlg(0);
     SetTopWindow(&Dlg);
     Dlg.ShowModal();
     wxsOK = false;
    }
    //*)
    return wxsOK;
}

This kind of code compiles fine, but when you press the close button on the program, it returns status -1. I asked help from Freenode's wxWidgets' channel and the guys helped me immediately (thanks DavidGH and BrianHV). I checked my code with Call Stack and for some reason the program seems that it just closes and it's not getting destroyed.

All I did was to replace my previous original code with this in wxDialogDefaultApp.cpp

Code
bool wxDialogDefaultApp::OnInit()
{
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if (wxsOK)
    {
        wxDialogDefaultDialog * Dlg = new wxDialogDefaultDialog(NULL);
        SetTopWindow(Dlg);
        Dlg->Show(true);
    }
    return wxsOK;
}

and in wxDialogDefaultMain.cpp I changed the Close() to Destroy().
Code
void wxDialogDefaultDialog::OnQuit(wxCommandEvent& event)
{
    Destroy();
}

Successfully compiled, and tested; it closes with status 0.

Can someone test the original code on other systems and provide feedback? Even though I have mentioned that is under wxMSW, it would not harm anyone by testing it on other environments as well.

My specs are:

Windows XP Professional SP3 (x86)
TDM's GCC 4.5.0
Code::Blocks svn-6386

P.S.: Can we add the syntax highlighter mod so we can make our sample code(s) here more attractive for the eye?
« Last Edit: July 18, 2010, 12:34:23 am by stefanos_ »

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: wxMSW Bug in default Dialog source code made with Wizard
« Reply #1 on: July 18, 2010, 08:33:45 am »
Quoting wxWidgets doc-
Quote
Closing Windows
Your application can either use wxWindow::Close event just as the framework does, or it can call wxWindow::Destroy directly. If using Close(), you can pass a true argument to this function to tell the event handler that we definitely want to delete the frame and it cannot be vetoed.
The advantage of using Close instead of Destroy is that it will call any clean-up code defined by the EVT_CLOSE handler; for example it may close a document contained in a window after first asking the user whether the work should be saved. Close can be vetoed by this process (return false), whereas Destroy definitely destroys the window.

Basically if you are sure that you don't want to do any further processing (saving open file, etc), use Destroy()

Another way to solve your problem is to pass true while calling Close() function.
Be a part of the solution, not a part of the problem.

stefanos_

  • Guest
Re: wxMSW Bug in default Dialog source code made with Wizard
« Reply #2 on: July 18, 2010, 12:20:23 pm »
Thank you for your reply.

I have tried what you have said and again returns status -1.

My code works as it supposed to work, returning status 0. By rewriting my code as it was originally composed by the wizard, it causes status -1 even if I use Close(true);.

Can anyone help a bit? I'm still at the learning process...

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: wxMSW Bug in default Dialog source code made with Wizard
« Reply #3 on: July 18, 2010, 11:59:41 pm »
I suggest using Destroy in OnInit.
The original code destroys the dialog implicitely when leaving the if-clause, the following code does it explicitely, and further processing can be done before calling Destroy().
And we still can use a modal dialog as in the actual wizard.

Code
bool testApp::OnInit()
{
    //(*AppInitialize
    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    testDialog* Dlg = new testDialog(0);
    SetTopWindow(Dlg);
    Dlg->ShowModal();
    Dlg->Destroy();
    }
    //*)
    return wxsOK;

}

stefanos_

  • Guest
Re: wxMSW Bug in default Dialog source code made with Wizard
« Reply #4 on: July 19, 2010, 11:10:10 am »
Jens, thank you for your suggestion.

This is exactly what I did last night after I read a few examples from zetcode.

Now everything works just fine.

Thanks for once again.

Regards,

stefanos_