无法删除MFC应用程序中的内存泄漏 [英] Can't remove memory leaks from MFC application

查看:111
本文介绍了无法删除MFC应用程序中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MFC SDI应用程序,其左侧为树状视图(浏览器样式).
我正在尝试确保没有内存泄漏,并且正在使用以下代码来做到这一点:

I have an MFC SDI app with a tree-view on the left (explorer-style).
I am trying to ensure there are no memory leaks and am using the following code to do so:

#ifdef _DEBUG
   		CMemoryState oldMemState, newMemState, diffMemState;
   		oldMemState.Checkpoint();
#endif
#ifdef _DEBUG
   		newMemState.Checkpoint();
   		if( diffMemState.Difference( oldMemState, newMemState ) )
   		{
			TRACE("Memory leaked!\n" );
      		AfxMessageBox(L"Memory leaked!\n" );
   		}
#endif


我发现我无法摆脱内存泄漏的问题,例如在打开填充树视图的文件(CTreeCtrl)和关闭文件并清空树视图的过程之间,存在内存泄漏.我删除所有树项,并在指向每个树项中保存的数据结构的所有指针上调用delete,但是在某处仍然存在一些内存泄漏.我通过CMyDoc类中的MFC消息处理程序调用打开和关闭.然后将内存检查放在OnOpenDoc方法的开头和OnCloseDoc方法的结尾.

我想知道使用MFC时是否存在不可避免的泄漏,因为发生了一些我无法控制的事情.

另外,任何人都可以告诉我在CMemeoryState结构中要查看哪些数字,以便随时查看使用了多少内存.


I am finding that I cannot get rid of memory leaks e.g. there is a memory leak between when I open a file that populates the tree-view (CTreeCtrl) and when I close the file and empty the tree-view. I delete all the tree items and call delete on all the pointers to the data structs held within each tree item, but there is still some memory leak somewhere. I call the open and close via MFC message handlers in the CMyDoc class. and I put the memory checks at the start of the OnOpenDoc method and at the end of the OnCloseDoc method.

I am wondering if there are unavoidable leaks when using MFC as there is stuff going on that I am not controlling.

Also, can anyone tell me which numbers to look at in the CMemeoryState struct to see how much memory is used at any time.

Is there a better (free) way to check for memory leaks in my case?

推荐答案

您最好查看代码的内存泄漏.查看泄漏的内存的内容.我猜您不会删除所有分配的子对象.您不会在所有指向数据结构的指针上调用delete" ;-)

在树中,您需要递归地工作,需要多加注意.
you better look at the memory leaks of your code. Look in the content of the leaked memory. I guess that you dont delete all allocated sub-objects. You wont "call delete on all the pointers to the data structs" ;-)

In a tree you need to work recursly what needs a lot of care.


获取消息内存泄漏",并不意味着您存在内存泄漏.这意味着那时的内存消耗只有一些差异.使用MFC时,没有不可避免的泄漏,可能会消耗一些内存.例如,请参见下面的代码

Getting the message "Memory leaked", doesnt mean that you have a memory leak. It means that only some difference is there in memory conumption at that time. When using MFC there are no unavoidable leaks, may be there is some memory consumption. for example see the below code

CMemoryState new, old, diff;
old.CheckPoint();
m_csMyStr.LoadString( IDS_MYSTRING );
old.CheckPoint();
if( diff.Difference( old, new ))
{
    AfxMessageBox( "Mem Leak" );
}



它给出消息"Mem Leak".但是程序员是否需要解决任何内存泄漏问题.没事吧?

但您需要确保所有new完成的操作,只需执行delete
如果调试代码,则可以在Visual Studio IDE本身中找到内存泄漏.在调试窗口中.



It gives the message "Mem Leak". But is there any memory leak that the programmer needs to resove. No right?

but you need to make sure that what all you have newed, you just need to delete
If you debug your code, you can find the memory leak in visual studio IDE itself. In debug window.


这篇关于无法删除MFC应用程序中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆