简体   繁体   English

如何通过Valgrind或其他工具检测我的Qt软件中的内存泄漏?

[英]How to detect memory leak in my Qt software by Valgrind or other tools?

I have developed a library with Qt/C++ and now I want to sure about memory leak testing, 我已经使用Qt / C ++开发了一个库,现在我想确定一下内存泄漏测试,

I found Valgrind and seems a good detector(I still don't work with it), but is there another tool(s) for testing for memory leak? 我找到了Valgrind,它似乎是一个很好的检测器(我仍然不使用它),但是是否还有另一个工具可以测试内存泄漏?

Yes, as Als has pointed out in a comment and from my personal experience, I would also recommend going with valgrind. 是的,正如艾尔斯(Als)在评论中指出的那样,并且根据我的个人经验,我也建议您使用valgrind。 There are various options such as --leak-check=yes etc. that you might use. 您可以使用各种选项,例如--leak-check=yes等。 Once you run valgrind, it outputs some recommend options that you can include in the next run. 运行valgrind后,它将输出一些推荐选项,您可以在下一次运行中包括这些选项。

The problem Valgrind is attempting, ie, of finding memory leaks, is a complex problem. Valgrind正在尝试的问题(即查找内存泄漏)是一个复杂的问题。 Sometimes valgrind gets confused and outputs false positives, ie, it shows a memory leak at a place where there is none. 有时valgrind会感到困惑并输出误报,即,它表明在没有内存泄漏的地方出现内存泄漏。 But, other than this, valgrind is quite user-friendly and useful. 但是,除此之外,valgrind非常用户友好和有用。

You could do the memory leak check yourself without much additional affort (depending on your code). 您可以自己进行内存泄漏检查,而无需付出额外的努力(取决于您的代码)。 Just provide your own versions of the operators new and delete. 只需提供自己的运营商版本即可new和delete。 Use a container to store each memory address that is assigned within new. 使用容器存储在new中分配的每个内存地址。 Remove it from the collection if delete is called. 如果调用delete,则将其从集合中删除。 At the end of your program, check if the collection is empty. 在程序结束时,检查集合是否为空。

Details can be eg found in Scott Meyers book Effective C++, Item 50. 有关详细信息,请参见Scott Meyers的有效C ++项目50。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM