Author Topic: Program hangs on exit when using wxMultiChoiceDialog with wxSmith  (Read 9703 times)

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1630
I have used common dialogs with wxSmith before without any problem, but today I have used wxMultiChoiceDialog and the program hanged on exit.

Checking the dialog flow I have noticed that the common dialogs (FileDialog, DirDialog...) are created by wxSmith but they are never destroyed. Adding a call to wxMultiChoiceDialog->Destroy() in the main frame destructor solves the hang issue.

I think wxSmith should call Destroy() of the common dialogs it creates, otherwise we are exposed to hangs and memory leaks.
« Last Edit: June 12, 2019, 04:20:54 pm by Miguel Gimenez »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6025
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
« Reply #1 on: June 12, 2019, 05:36:29 pm »
How do you use the dialog? Does wxsmith generate the creating code of a dialog?
For me, I just use wxsmith to generate a dialog header file and implementation file, and all the creating and destroying code are added manually by hand.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1630
Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
« Reply #2 on: June 12, 2019, 05:56:23 pm »
If you have a frame (generated with wxSmith) and add a wxMultiChoiceDialog to the frame (using wxSmith again) you end with this:

Frame constructor (some code removed for clarity)

Code
    //(*Initialize(FichaFrame)
    Create(parent, wxID_ANY, _("Test"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, "wxID_ANY");
    wxString __wxMultiChoiceDialogChoices_1[3] =
    {
        _("PDF"),
        _("CSV"),
        _("HTML")
    };

    MultiChoiceDialog1 = new wxMultiChoiceDialog(this, _("A"), _("B"), 3, __wxMultiChoiceDialogChoices_1, wxCHOICEDLG_STYLE|wxOK|wxCANCEL|wxCENTRE, wxDefaultPosition);
    //*)

Frame destructor

Code
    //(*Destroy(FichaFrame)
    //*)

The wxSmith code in the frame destructor should be

Code
    //(*Destroy(FichaFrame)
    MultiChoiceDialog1->Destroy();
    //*)

THe lack of destruction is OK for things like wxButtons and the such, as they are owned and destroyed by containers, but not for common dialogs. FileDialog, DirDialog and others suffer the same problem.

I have been looking in wxSmith's code and there is no provision for filling the destructor part (something like OnBuildDestroyingCode()).


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
« Reply #3 on: June 12, 2019, 07:12:23 pm »
But this usage of these dialogs isn't really good. They are meant to be used locally and to have very little scope. I don't think it is a job of wxSmith to manage these dialogs.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
« Reply #4 on: June 12, 2019, 09:50:18 pm »
Quote
Code: [Select]

    //(*Destroy(FichaFrame)
    //*)


The wxSmith code in the frame destructor should be

Code: [Select]

    //(*Destroy(FichaFrame)
    MultiChoiceDialog1->Destroy();
    //*)

the code to construct this is in the pipeline :) (if this is the correct way to use the dialog is a other question that i can not answer

Offline sodev

  • Lives here!
  • ****
  • Posts: 500
Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
« Reply #5 on: June 12, 2019, 10:16:57 pm »
I'm not familiar with wxSmith but why / how can you add a top-level element inside another top-level element? This shouldn't be possible at all, after all the dialog is not part of the layout of the frame but an individual element by itself.

And like oBFusCATed already said, these modal dialogs are not designed for a longer lifetime, they get created locally on the stack, you call their usually single method that returns a result and then they get deleted by the block scope. And all this happens inside user code and not the layout code created by a gui builder.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1630
Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
« Reply #6 on: June 13, 2019, 11:58:40 am »
The solution may be fixing them (thanks BlueHazzard), removing them if they have no sense or add a note in a "Known issues" section in the documentation. The memory leak is a minor problem, the hang on closing is.

They have little sense in wxSmith, but they are handy for one-frame small utilities. Other items strange in wxSmith are some in the Tools section: wxTimer, wxSingleInstanceChecker, wxStopWatch... In their case there is no problem with destruction, because they are not created in the heap.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1630