查找内存泄漏-成员在CMemoryState结构中是什么意思? [英] Finding memory leaks - What do the members mean in CMemoryState structure?

查看:70
本文介绍了查找内存泄漏-成员在CMemoryState结构中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMemoryState检查内存泄漏,如下所示:

I''m using the CMemoryState to check for memory leaks as follows:

#ifdef _DEBUG
   		CMemoryState oldMemState, newMemState, diffMemState;
   		oldMemState.Checkpoint();
#endif

//put method to check in here

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



有人可以告诉我该结构如何用于检查泄漏.我在检查时一直在查看成员,并且似乎diffMemState中唯一一个表示非零内存泄漏的成员是m_lCounts [1].如果其他m_lCounts []中的任何一个都不为零,那么这似乎并不意味着存在内存泄漏-是正确的,它们的含义是什么?



Can somebody tell me how the struct is used to check for leaks. I have been looking at the members as I do my check and it seems that the only member in diffMemState that indicates a memory leak when it is non-zero is m_lCounts[1]. If any of the other m_lCounts[] are non-zero, it doesn''t seem to mean that there is a memory leak - is that correct, and of so what do they mean?

推荐答案

还有另一个有用的调用,可以添加到您的代码中,
There is another useful call to add to your code,
diffstate.DumpStatistics();<br />

应该告诉您第一个检查点和第二个检查点之间仍然分配的内容.

现在,从前面的注释中,您应该意识到,并非所有仍分配"的对象/内存都是泄漏"的.它可能只是您仍在使用的某些对象.例如,我的应用程序缓存从文件读取的数据.如果我在读取数据之前先经过检查点,然后检查泄漏",则缓存的数据将显示为仍已分配".那是泄漏"吗,不是因为我知道它,它是设计的一部分.

因此,在TRACE消息后使用"DumpStatistics"以查看仍分配了哪些内存.如果您具有默认的DEBUG_NEW分配,则还应该看到分配该内存的模块和行号.您甚至可以双击输出,Visual Studio应该将您带到进行分配/新建的行.

That should tell you of the things that remain allocated between the first checkpoint and the second checkpoint.

Now from previous notes, you should realize that not all "still allocated" objects / memory are "leaks". It might simply be some object that you are still using. For example, my application caches data read from a file. If I were to take the checkpoint before reading the data and then check for "leaks", the cached data would appear as "still allocated". Is that a "leak", no because I know about it and it was part of the design.

So use the "DumpStatistics" after your TRACE message to see what memory is still allocated. If you have the default DEBUG_NEW allocation, you should also see the module and line number where that memory was allocated. You might even be able to double-click on the output and Visual Studio should take you to the line where the allocation / new occurred.


这篇关于查找内存泄漏-成员在CMemoryState结构中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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