CMemoryState主要功能是什么
答案:2 悬赏:30 手机版
解决时间 2021-04-06 03:04
- 提问者网友:黑米和小志
- 2021-04-05 16:43
CMemoryState主要功能是什么...急..........
最佳答案
- 五星知识达人网友:千杯敬自由
- 2021-04-05 17:19
使用CMemoryState检测内存泄漏 CMemoryState是Visual C++提供的内存检测类,主要用于检测程序中的内存泄漏。CMemoryState具有以下成员函数:CMemoryState::Checkpoint() 设置检测点 CMemoryState::Difference() 比较两个CMemoryState对象 CMemoryState::DumpAllObjectsSince() dump检测点以后所有的对象 CMemoryState::DumpStatistics() 打印内存统计信息 CMemoryState::Checkpoint() 设置检测点 CMemoryState::Difference() 比较两个CMemoryState对象 CMemoryState::DumpAllObjectsSince() dump检测点以后所有的对象 CMemoryState::DumpStatistics() 打印内存统计信息使用CMemoryState检测内存泄漏的步骤很简单,先准备两个对象分别在程序的首尾位置设置检测点,然后调用Difference()得出它们的差异,并将差异打印出来,程序员即可得知程序中内存泄漏的程度。以下是使用CMemoryState的例子,注意观察:查看源代码 拷贝至剪贴板 打印代码void foo() { CMemoryState ms1, ms2, ms3, msd; ms1.Checkpoint(); int *ip = new int [100]; ms2.Checkpoint(); delete [] ip; ms3.Checkpoint(); msd.Difference( ms1, ms2); msd.DumpStatistics(); //未释放内存 TRACE("\r\n"); msd.Difference( ms1, ms3); msd.DumpStatistics(); //已释放内存 } void foo() { CMemoryState ms1, ms2, ms3, msd; ms1.Checkpoint(); int *ip = new int [100]; ms2.Checkpoint(); delete [] ip; ms3.Checkpoint(); msd.Difference( ms1, ms2); msd.DumpStatistics(); //未释放内存 TRACE("\r\n"); msd.Difference( ms1, ms3); msd.DumpStatistics(); //已释放内存 } 其Debug结果如下:0 bytes in 0 Free Blocks. 400 bytes in 1 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 400 bytes. Total allocations: 400 bytes. 0 bytes in 0 Free Blocks. 0 bytes in 0 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 400 bytes. Total allocations: 400 bytes. 0 bytes in 0 Free Blocks. 400 bytes in 1 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 400 bytes. Total allocations: 400 bytes. 0 bytes in 0 Free Blocks. 0 bytes in 0 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 400 bytes. Total allocations: 400 bytes.可以从输出数据发现,在delete [] p释放内存之前,系统存在内存泄漏(泄漏值为400 bytes),在释放之后,内存泄漏消失了。使用CMemoryState,可以有效地检测到程序中的内存泄漏。
全部回答
- 1楼网友:山河有幸埋战骨
- 2021-04-05 18:40
任务占坑
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯