MFC程序内存泄露检查
来源:互联网 发布:淘宝清仓的报名流程 编辑:程序博客网 时间:2024/05/16 10:13
CMemoryState可以帮助我们检测程序中的内存泄漏,具体定义为:
CMemoryState provides a convenient way to detect memory leaks in your program. A “memory leak” occurs when memory for an object is allocated on the heap but not deallocated when it is no longer required. Such memory leaks can eventually lead to out-of-memory errors. There are several ways to allocate and deallocate memory in your program:
- Using the malloc/free family of functions from the run-time library.
- Using the Windows API memory management functions, LocalAlloc/LocalFree andGlobalAlloc/GlobalFree.
- Using the C++ new and delete operators.
The CMemoryState diagnostics only help detect memory leaks caused when memory allocated using thenew operator is not deallocated usingdelete. The other two groups of memory-management functions are for non-C++ programs, and mixing them withnew anddelete in the same program is not recommended. An additional macro,DEBUG_NEW, is provided to replace thenew operator when you need file and line-number tracking of memory allocations.DEBUG_NEW is used whenever you would normally use thenew operator.
As with other diagnostics, the CMemoryState diagnostics are only available in debug versions of your program. A debug version must have the_DEBUG constant defined.
If you suspect your program has a memory leak, you can use the Checkpoint, Difference, and DumpStatistics functions to discover the difference between the memory state (objects allocated) at two different points in program execution. This information can be useful in determining whether a function is cleaning up all the objects it allocates.
具体实现方法为:
在APP定义成员变量
#ifdef _DEBUG //一定要在debug模式下搞啊
CMemoryState m_StPt, m_EndPt, m_Diff;
#endif //_DEBU
在InitInstance函数中添加
if (! AfxOleInit())
return FALSE;
#ifdef _DEBUG //一定要在debug模式下搞啊
m_StPt.Checkpoint();
#endif //_DEBUG
在ExitInstance函数中添加
int CTestApp::ExitInstance()
{
#ifdef _DEBUG//一定要在debug 模式下搞啊
m_EndPt.Checkpoint();
if (m_Diff.Difference(m_StPt, m_EndPt))
{
TRACE("\n-------- Detected memory leaks. DUMP(DumpStatistics) Start --------\n");
m_Diff.DumpStatistics();
TRACE("-------- Detected memory leaks. DUMP(DumpStatistics) End --------\n");
TRACE("\n-------- Detected memory leaks. DUMP(DumpAllObjectsSince) Start --------\n");
m_Diff.DumpAllObjectsSince();
TRACE("-------- Detected memory leaks. DUMP(DumpAllObjectsSince) End --------\n\n");
}
else
{
TRACE("\n-------- No detected memory leaks. OK !! --------\n\n");
}
#endif //_DEBUG
return CWinApp::ExitInstance();
}
完毕
- MFC程序内存泄露检查
- 检查MFC程序的内存泄露
- 检查MFC程序的内存泄露
- 检查MFC程序的内存泄露
- 检查MFC程序的内存泄露
- 检查MFC程序的内存泄露
- MFC内存泄露检查
- Win32程序检查内存泄露
- 在vs2008中检查MFC内存泄露
- 用UMDH检查C++程序内存泄露
- c程序内存泄露检查工具
- 用UMDH检查C++程序内存泄露
- 关于MFC下检查和消除内存泄露的技巧
- 关于MFC下检查和消除内存泄露的技巧
- 关于MFC下检查和消除内存泄露的技巧
- 关于MFC下检查和消除内存泄露的技巧
- 关于MFC下检查和消除内存泄露的技巧
- 关于MFC下检查和消除内存泄露的技巧
- linux yum怎么安装 及过程
- keystone 分层多租户
- Foundation 之 NSValue
- Mysql创建触发器实现不同表的插入、更新、删除操作
- 求连续数串和最大的起始、结束位置以及最大值
- MFC程序内存泄露检查
- android 读取联系人实例
- 接口层输入
- hadoop 2.7.1 配置后使用异常
- 用数组取指定模式地址的内容
- 程序47 读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
- 【2015/11/1】C学习日志_Day11&12 数据类型 指针 内存对齐 函数指针
- Unity3d之UGUI- Image拦截Button响应事件
- Weblogic节点启动失败及其原因