无法删除内存,请帮忙看看为什么

stone13525435056 2010-01-16 11:45:32
#define WXUSINGDLL
#include <wx/wx.h>
#include <iostream>

using namespace std;

/*********定义自己的事件处理类******************/
class MyEventHandler : public wxEvtHandler
{
public:
MyEventHandler(){};
~MyEventHandler(){};

void OnAbout(wxCommandEvent& event);

};

void MyEventHandler::OnAbout(wxCommandEvent& event)
{
cout << "It is my Eventhandler::OnAbout!" << endl;
}

/**********************************************/

class MyApp : public wxApp
{
public:
virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
virtual ~MyFrame(){};

void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);

private :
MyEventHandler *m_eventHandler;

DECLARE_EVENT_TABLE()
};

DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));
frame->Show(true);

return true;
}

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
END_EVENT_TABLE()

void MyFrame::OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf(wxT("Hello and welcome to %s"),
wxVERSION_STRING);

wxMessageBox(msg,wxT("About Minimal"),
wxOK | wxICON_INFORMATION, this);
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
Close();

/*******无法删除**************/
PopEventHandler(TRUE);

delete m_eventHandler;
m_eventHandler = NULL;
/******************************/
}

MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxPoint(0,0),wxSize(800,600))
{
m_eventHandler = NULL;
wxMenu *fileMenu = new wxMenu;
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
wxT("Show about dialog"));
fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt.X"),
wxT("Quit this program"));

wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, wxT("&File"));
menuBar->Append(helpMenu, wxT("&Help"));

SetMenuBar(menuBar);

wxButton* button = new wxButton(this,wxID_OK,wxT("OK"),wxPoint(200,200));

CreateStatusBar(3);

SetStatusText(wxT("Welcome to wxWidgets!"));

m_eventHandler = new MyEventHandler();

m_eventHandler->Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(MyEventHandler::OnAbout));

PushEventHandler(m_eventHandler); //加载事件处理函数

}
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
traceless 2010-01-17
  • 打赏
  • 举报
回复
代码太长了 而且还没有调试代码,自己debug吧

照你的描叙,就是有访问冲突了,有个地方正在使用m_eventHandler。。

stone13525435056 2010-01-17
  • 打赏
  • 举报
回复
OnQuit 函数里 走到 delete m_eventHandler 弹出一个异常访问

如果我把delete m_eventHandler 放入 MyFrame的析构函数甚至没有走到析构函数就异常了

还有一点很奇怪,如果不delete就不会有异常,但是我在析构函数里打断点,一直走不进去,请指教
stone13525435056 2010-01-17
  • 打赏
  • 举报
回复
哈哈 找到原因了
PopEventHandler()函数里有个局部指针变量handlerA,和m_eventHandler指向同一个内存,PopEventHandler里删除handlerA指向的内存然后把handlerA=NULL,这时eventHandler变为了野指针,
于是 delete m_eventHandler......,感谢感谢!
traceless 2010-01-16
  • 打赏
  • 举报
回复
怎样个无法删除 ?

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧