在Debug状态下测试内存泄漏问题:
// Declare the variables needed
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
// do your memory allocations and deallocations...
#ifdef _DEBUG
newMemState.Checkpoint();
if(diffMemState.Difference( oldMemState, newMemState ) )
{
TRACE( "\n\n\nAttention please: Memory leaked!\n\n\n" );
}
else
{
TRACE( "\n\n\nCongratulations: No memory leaked!\n\n\n" );
}
#endif
// Declare the variables needed
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
// do your memory allocations and deallocations...
#ifdef _DEBUG
newMemState.Checkpoint();
if(diffMemState.Difference( oldMemState, newMemState ) )
{
TRACE( "\n\n\nAttention please: Memory leaked!\n\n\n" );
}
else
{
TRACE( "\n\n\nCongratulations: No memory leaked!\n\n\n" );
}
#endif
解决方案 »
- vs2008中源文件的编码方式,有没地方能全局设置?
- 我的程序从添加删除程序列表里面删除后(直接删除注册表的方法),列表更新很慢(要10S),基本不能动。
- 招在校大学生培养做后备,培养你的实际开发能力
- 急求解答:在類似VC的界面中,左邊的樹形控件不是添加一個控件,而是在MainFrm中定義的一個變量,如何做它的雙擊響應事件
- 这样添加菜单,对不对呀,怎么添加不上???????
- 简单的问题
- 使用Xtreme Toolkit时发生的一个Assertion
- 如何调用dll
- WIN32ASM中如何复制一个包含子目录及文件的目录??
- 如何让程序自动[以管理员身份]运行
- 我的2000怎么连不上网?网上邻居能看到别人的机器,我们是用sygate拨号上的网?我已经输入了网关的IP地址,是不是要配DNS啊?请大家指教!
- DSOUND的问题!
可以利用CMemoryState类帮助检测内存泄漏,例如:
#ifdef _DEBUG
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
#endif
...
(被测试的代码)
...
#ifdef _DEBUG
newMemState.Checkpoint();
if(diffMemState.Difference(oldMemState, newMemState)) {
TRACE(“Memory Leaked Here:\n\n" );
}
#endif
如果没有使用MFC呢?1。写自己的内存分配和释放函数:
C就写mymalloc和myfree,然后使用它们。C++ 就重载全局new 和 delete。
2。_onexit()!