1

I've read this, but I still don't understand what I did wrong with the code.

Here's the bmNewFromImageDialog.h, the class that extends wxDialog.

#ifndef BMNEWFROMIMAGEDIALOG_H
#define BMNEWFROMIMAGEDIALOG_H

#include "./../utils/includer.h" // includes a bunch of wx-related files
class bmNewFromImageDialog : public wxDialog {
public:
    wxBoxSizer *mainVBox, *flagHBox, *OKCancelHBox;
    wxStaticBox *flagsSBox;
    wxButton *OKButton, *cancelButton;
    wxRadioButton *GT0RadioButton;

    bmNewFromImageDialog() {}
    bmNewFromImageDialog(wxWindow *parent);

    void init();
};

#endif

Here's the bmNewFromImageDialog.cpp.

#include "./bmNewFromImageDialog.h"
#include "./../utils/includer.h"

bmNewFromImageDialog::bmNewFromImageDialog(wxWindow *parent) : wxDialog(parent, -1, wxT("new image..."), wxDefaultPosition, wxDefaultSize) {
    init();
}

void bmNewFromImageDialog::init() {
    mainVBox = new wxBoxSizer(wxVERTICAL);
    flagHBox = new wxBoxSizer(wxHORIZONTAL);
    OKCancelHBox = new wxBoxSizer(wxHORIZONTAL);

    flagsSBox = new wxStaticBox(this, -1, wxT("Color flags"), wxPoint(0, 0), wxDefaultSize);
    GT0RadioButton = new wxRadioButton(this, -1, wxT(">0: 3 channels"), wxPoint(0, 0));
    flagsSBox->AddChild(GT0RadioButton);
    flagHBox->Add(flagsSBox, 0, wxALIGN_CENTER | wxTOP | wxBOTTOM, 10);

    OKButton = new wxButton(this, wxID_OK, wxT("OK"), wxDefaultPosition, wxSize(100, 40));
    cancelButton = new wxButton(this, wxID_CANCEL, wxT("cancel"), wxDefaultPosition, wxSize(100, 40));
    OKCancelHBox->Add(OKButton, 1);
    OKCancelHBox->Add(cancelButton, 1, wxLEFT, 5);

    mainVBox->Add(flagHBox, 0, wxALIGN_CENTER | wxTOP | wxBOTTOM, 10);
    mainVBox->Add(OKCancelHBox, 0, wxALIGN_CENTER | wxBOTTOM, 10);
    SetSizer(mainVBox);
    Center();
}

and this is how I use the dialog in code:

bmNewFromImageDialog *newDialog = new bmNewFromImageDialog(this);
newDialog->ShowModal();
delete newDialog;

I constantly get an unhandled memory exception error at runtime when I close / click OK / click Cancel on that bmNewFromImageDialog. How should I fix this?

EDIT

@sir-digby-chicken-caesar I tried the .Destroy() and the stack solution, but there's still the same error:

enter image description here

hello all
  • 252
  • 2
  • 17

2 Answers2

0

This block:

bmNewFromImageDialog *newDialog = new bmNewFromImageDialog(this);
newDialog->ShowModal();
delete newDialog;

Should be changed to this:

bmNewFromImageDialog *newDialog = new bmNewFromImageDialog(nullptr);
newDialog->ShowModal();
newDialog->Destroy();

Never call delete on a wx widget class. If you need to deallocate a widget, remove it from the parent hierarchy, detach it from any sizer that uses it then call Destroy();

Edit:

As pointed out by user VZ and the wxWidgets documentation, modal dialogs are one of the few wx widget classes that don't need to be created on the heap. So you could just create the dialog on the stack and let the destructor handle cleanup for you once the object goes out of scope:

bmNewFromImageDialog newDialog = bmNewFromImageDialog(nullptr);
newDialog.ShowModal();
Sir Digby Chicken Caesar
  • 3,063
  • 1
  • 23
  • 31
0
flagsSBox = new wxStaticBox(this, -1, wxT("Color flags"), wxPoint(0, 0), wxDefaultSize);
GT0RadioButton = new wxRadioButton(this, -1, wxT(">0: 3 channels"), wxPoint(0, 0));
flagsSBox->AddChild(GT0RadioButton);
//         ^^^^^^^^ this looks very wrong

That call is undocumented, and marked as "implementation mostly" in the header file. Do not use that unless you really know what you are doing.

It looks like your code should work if you just create GT0RadioButton with flagsSBox as its parent (remove the AddChild call).

Take a look at wxStaticBoxSizer too.

catalin
  • 1,927
  • 20
  • 20