InitInstance中创建CWnd派生的窗口对象,内存泄漏
来源:互联网 发布:手机处理数据软件 编辑:程序博客网 时间:2024/05/29 15:06
问题
MFC程序中,在主程序类的InitInstance中创建由CWnd派生的CMyWnd窗口对象,程序关闭时,在vs的输出窗口报告有内存泄漏。
分析
单文档、多文档程序都是在InitInstance中new出主窗口,我也是仿照那个写的。但存在区别,CMyWnd类是从CWnd类派生,而前者是派生自CFrameWnd。
那原因就从CFrameWnd身上找吧,方法:在自动生成的单文档程序的主窗口类的析构函数中下个断点,关闭程序,从函数调用堆栈中即可看出是哪里删除这个主窗口对象的,查找到以下函数:
void CFrameWnd::PostNcDestroy(){ // default for frame windows is to allocate them on the heap // the default post-cleanup is to 'delete this'. // never explicitly call 'delete' on a CFrameWnd, use DestroyWindow instead delete this;}
MSDN对PostNcDestroy的说明也证实了这一点:
Called by the default OnNcDestroy member function after the window has been destroyed. Derived classes can use this function for custom cleanup such as the deletion of the this pointer.
解决方案:
首先看下OnNcDestroy的文档说明:
Override PostNcDestroy if you want to perform your own cleanup, such as a delete this operation. If you override OnNcDestroy, you must call OnNcDestroy in your base class to ensure that any memory internally allocated for the window is freed.
这里给出的解决办法,重载虚函数PostNcDestroy去删除本身
- InitInstance中创建CWnd派生的窗口对象,内存泄漏
- MFC中创建或派生基于CWnd的类
- MFC中CWnd对象和Windows窗口的区别
- CWnd直接派生的窗口下使用CToolBar笔记
- VC++ 在CXXApp的InitInstance()函数中获取窗口句柄
- MFC CWinApp派生类中使用CFrameWnd派生类的成员函数Create来创建窗口
- MFC中CWnd类及其派生类对话框、消息处理、窗口操作
- MFC中CWnd类及其派生类对话框、消息处理、窗口操作
- C++中派生类对象的内存布局
- 浅谈C++中派生类对象的内存布局
- 对话框上创建CWnd 窗口
- CWnd派生类添加双表双击消息 需要注册窗口属性里面的 CS_DBLCLKS
- CWnd创建WS_CHILD和WS_POPUP窗口的不同
- 用CWnd创建一个没有父窗体的POPUP窗口
- CWnd类的对象和Windows窗口之间的关系
- Android中常见的内存泄漏之上下文对象
- 关于MFC DLL CWinApp::InitInstance()中创建线程的问题 易造成死锁
- 内存泄漏,当您使用的 GetDC 方法和 ReleaseDC 方法 CWnd 类版本
- ULUA的简洁用法(二)
- Lucene教程--Analyzer分析器详解
- js判断鼠标移入的方向并触发事件
- Golang 学习之路四:类型
- 转:kafka数据可靠性深度解读
- InitInstance中创建CWnd派生的窗口对象,内存泄漏
- 第十周:( Sicily1091) Maximum Sum(c++)
- 开启子线程的方法
- java语言基础(42)——package关键字概述和使用
- 【MVC】-走进你的世界
- JAVA反射机制(一)
- php中序列化与反序列化
- 流-输入输出流
- xhEditor用法