简体   繁体   中英

wxWidgets wxTextCtrl crash when deleting

wxTextCtrl causes some memory allocation problem when tried to be deleted or it's value changed. Here's some code insight:

    wxTextCtrl* s = new wxTextCtrl(...);
    s->SetValue("abc");//crash
    delete s//crash

It's like all of its members are const's. Here is what VisualStudio says when it crashes:

    An unhandled exception of type 'System.AccessViolationException' 
    occurred in Unknown Module.

    Additional information: Attempted to read or write protected memory. 
    This is often an indication that other memory is corrupt.

And even when i try the wxWidgets default destroy:

    parent->DestroyChildren(); //ofc the parent is wxPane passed in constructor of s

Any help will be appreciated.

Here's some actual code from the only functions calling wxTextCtrl:

  void AddButton::OnAction(wxSize* frame){

if ( !DoAction ){
    if ( ! thy )
    {

        thy = new wxPanel
            (mParent, -1, 
            wxPoint(0, 0),
            wxSize(PanelWidth, mParent->GetSize().GetHeight()), 
            wxBORDER_NONE | wxFRAME_FLOAT_ON_PARENT );
        thy->SetBackgroundColour(wxColor(30,30,30));
        thy->Show();
        if ( ! AddPanelDialog ){
            //AddPanelDialog = (new _Text
                //(this, thy, "add link...", wxPoint(1, 30), wxSize(PanelWidth - 30, 20),
                //wxBORDER_NONE | wxTE_PROCESS_ENTER ));
            wxTextCtrl* s = new wxTextCtrl(thy, -1, "", wxPoint(1, 30), wxSize(PanelWidth - 30, 20),
                wxBORDER_NONE | wxTE_PROCESS_ENTER );
            s->Connect(wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler(_Text::OnEnter));
            s->Show();
        }
        if ( !ConfirmPanel ){
            ConfirmPanel = new wxPanel
                (thy, -1, wxPoint(PanelWidth - 28, 30), wxSize(27, 20), 
                wxBORDER_NONE | wxFRAME_FLOAT_ON_PARENT );
            ConfirmPanel->SetBackgroundColour(wxColor(38, 145, 232));
            ConfirmPanel->Show();
        }

    }
    else {
        thy->Show();
    }
    gui* rmd = (gui*)mParent;
    rmd->LeftPanelActivate();
    rmd->SetNewPositions(rmd->GetParent()->GetSize());
    Button::Update();
    helper::SendRedrawEvent(mParent);
    DoAction = true; // indicates action activated
}
else{
    thy->Hide();
    gui* rmd = (gui*)mParent;
    rmd->LeftPanelActivate(false);
    rmd->SetNewPositions(rmd->GetParent()->GetSize());
    Button::Update();
    helper::SendRedrawEvent(mParent);
    DoAction = false; // indicates action activated
}
    }

and function that calls SetValue()

   void AddButton::OnEnter(wxCommandEvent& event)//enter button handler
   {
       wxTextCtrl* _t = (wxTextCtrl*)this;
       _Clear();
       *_t<<"sup";
   }

Are you sure that you really need to delete the wxTextCtrl? If this text control is placed into sizer then sizer is responsible for it and will destroy it when needed. You probably need to detach the text control from sizer and then delete it. Also you should use Destroy() method instead of delete operator. This is clearly explained in docs.

As for crash on SetValue() call: have you tried to use wxT("abc") ? Also what version of wxWidgets, OS and compiler are you using? Haven't experienced such problems with wxWidgets at all. Maybe you could post some meaningful piece of code which can help to identify the problem?

I think you have a problem with understanding of Connect() . If you're using it to connect to a method of a different object, you must pass a pointer to this object as the last argument (called eventSink in the documentation ). Your method is almost certainly being called on a wrong object pointer.

And you should absolutely never, ever have to cast this like you do in OnEnter() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM