Page 1 of 1

wxFileDialog Works on Linux, Hangs on Windows

Posted: Tue Nov 15, 2011 5:02 am
by Xangis
I have a mostly-command-line application that uses a wxFileDialog as a file chooser at one point (to select a file to upload via FTP). This works great on Linux, but on Windows it hangs when I call ShowModal() on wxFileDialog. There is no root window in this application and I have no idea whether that's a factor in the hang.

Here's the code I use:

Code: Select all

std::string BrowseForFileUpload(const char*)
{
    wxFileDialog* openFileDialog = new wxFileDialog(NULL, _("Send File"), wxEmptyString, wxEmptyString, _("All Files (*.*)|*.*"), wxFD_OPEN|wxFD_FILE_MUST_EXIST);
    if( openFileDialog->ShowModal() == wxID_CANCEL ) // App hangs on this line
    {
        delete openFileDialog;
        return "";
    }

    wxString filename = openFileDialog->GetPath();
    delete openFileDialog;
    return std::string(filename.mb_str());
}
I'm willing to fake a root window without showing it if it's required (but you'll need to tell me how), but showing an actual parent window is a no go.

Re: wxFileDialog Works on Linux, Hangs on Windows

Posted: Tue Nov 15, 2011 5:03 pm
by Radek
I have no experience with wxWidgets on winblows but:

(1) The wxFileDialog is itself a "top level window" because its parent is NULL. The dialog has no parent.
(2) You are trying to show this top level window as ShowModal(). This can cause troubles. It depends how modal dialogs are implemented by the operating system.

Moreover, do not delete the dialog. Use Destroy() instead. I recommend a modification:

Code: Select all

if( openFileDialog->Show() == wxID_CANCEL )
{
  openFileDialog->Destroy();
  return "";
}
else
{
  wxString filename = openFileDialog->GetPath();

  openFileDialog->Destroy();
  return std::string(filename.mb_str());
}

Re: wxFileDialog Works on Linux, Hangs on Windows

Posted: Wed Nov 16, 2011 1:17 am
by Xangis
Thank you. I apparently need to ShowModal() on OSX and Linux but not on Windows.

What's the fundamental difference between Destroy() and delete? What happens in Destroy() that doesn't happen in the destructor?

Re: wxFileDialog Works on Linux, Hangs on Windows

Posted: Wed Nov 16, 2011 2:42 pm
by Auria
Destroy schedules the delete for later but will first empty the event queue, which means that if there are events in the queue that refer to the window being deleted these events won't cause a crash when being processed

Re: wxFileDialog Works on Linux, Hangs on Windows

Posted: Sat Nov 19, 2011 6:17 am
by Radek
Note: If you ShowModal() then terminate the dialog by EndModal() instead of Destroy(). Use Destroy() only if you Show() a dialog or a frame.