gpt4 book ai didi

c++ - 无法从 OnExit() 中删除在 WxApp 中创建的动态对象

转载 作者:可可西里 更新时间:2023-11-01 09:59:11 26 4
gpt4 key购买 nike

我试图了解应该如何设计 WxWidgets (3.0.1) 应用程序,但我遗漏了一些东西,因为我正在尝试的东西不起作用。

在基本层面上,我有我的 WxApp,它创建了一个 WxFrame,它得到显示并且一切正常。然后我决定添加一个记录器对象....

我使该对象成为我的 WxApp 的成员:

class IniLogWx : public wxApp
{
public:
virtual bool OnInit( );
virtual int OnExit( );

private:
cLogStore * cl_LogStore;
};

并在 WxApp::OnInit() 中初始化它

bool IniLogWx::OnInit( )
{
MainFrame * frame = new MainFrame(_("Log Demo"), wxPoint(250, 250), wxSize(450, 340));
frame->Show(true);
SetTopWindow(frame);

// Create Logger Class
cLogStore * cl_LogStore = new cLogStore( );

return true;
}

当应用程序关闭(关闭 MainFrame)时,WxApp::OnExit() 触发,我认为我可以在这里清理我的内存

int IniLogWx::OnExit( )
{
delete cl_LogStore; // Unhandled Exception here due to invalid pointer

return wxApp::OnExit();
}

但是,指针似乎不再有效,所以我不能用它来删除我分配的内存。

手册说“在销毁所有应用程序窗口和控件之后,但在 wxWidgets 清理之前调用 OnExit。”所以我想我自己的额外属性仍然对我可用。

不是这样吗?

最佳答案

问题是您创建的局部变量恰好与您在类中使用的名称相匹配。

// Create Logger Class
cLogStore * cl_LogStore = new cLogStore( );

这里的cl_LogStore和成员变量cl_LogStore是不一样的。它是一个局部变量。因此,您不仅在无效指针值上调用了 delete,而且还发生了内存泄漏。

关于c++ - 无法从 OnExit() 中删除在 WxApp 中创建的动态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24446804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com