Code::Blocks

Developer forums (C::B DEVELOPMENT STRICTLY!) => Plugins development => Topic started by: Miguel Gimenez on June 12, 2019, 02:53:32 pm

Title: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: Miguel Gimenez on June 12, 2019, 02:53:32 pm
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.
Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: ollydbg 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.
Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: Miguel Gimenez 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: [Select]
    //(*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: [Select]
    //(*Destroy(FichaFrame)
    //*)

The wxSmith code in the frame destructor should be

Code: [Select]
    //(*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()).

Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: oBFusCATed 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.
Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: BlueHazzard 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
Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: sodev 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.
Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: Miguel Gimenez 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.
Title: Re: Program hangs on exit when using wxMultiChoiceDialog with wxSmith
Post by: Miguel Gimenez on June 18, 2019, 03:00:47 pm
Created ticket 845

https://sourceforge.net/p/codeblocks/tickets/845/