wxFileDialog Works on Linux, Hangs on Windows 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
Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

wxFileDialog Works on Linux, Hangs on Windows

Post 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.
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]
Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: wxFileDialog Works on Linux, Hangs on Windows

Post 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());
}
Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

Re: wxFileDialog Works on Linux, Hangs on Windows

Post 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?
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: wxFileDialog Works on Linux, Hangs on Windows

Post 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
"Keyboard not detected. Press F1 to continue"
-- Windows
Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: wxFileDialog Works on Linux, Hangs on Windows

Post 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.
Post Reply