【发布时间】:2013-03-17 22:32:31
【问题描述】:
wxTextCtrl 在尝试删除或更改其值时会导致一些内存分配问题。以下是一些代码见解:
wxTextCtrl* s = new wxTextCtrl(...);
s->SetValue("abc");//crash
delete s//crash
好像它的所有成员都是 const 的。这是 VisualStudio 在崩溃时所说的内容:
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.
即使我尝试使用 wxWidgets 默认销毁:
parent->DestroyChildren(); //ofc the parent is wxPane passed in constructor of s
任何帮助将不胜感激。
这是调用 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
}
}
以及调用 SetValue() 的函数
void AddButton::OnEnter(wxCommandEvent& event)//enter button handler
{
wxTextCtrl* _t = (wxTextCtrl*)this;
_Clear();
*_t<<"sup";
}
【问题讨论】:
-
你的代码的其他地方明显有bug,你必须明白,如果其他地方没有问题,调用
SetValue()不会崩溃。但是您没有告诉我们有关您的其余代码的任何信息,因此无法为您提供帮助。 -
@VZ。在那里我添加了更多代码。我正在使用 IMPLEMENT_APP 宏实现该应用程序。其他一切都像往常一样......
标签: c++ visual-studio-2010 wxwidgets