开发者

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

开发者 https://www.devze.com 2023-04-04 02:45 出处:网络
I have developed a library with Qt/C++ and now I want to sure about memory leak testing, I found Valgrind and seems a go开发者_Python百科od detector(I still don\'t work with it), but is there another

I have developed a library with Qt/C++ and now I want to sure about memory leak testing,

I found Valgrind and seems a go开发者_Python百科od detector(I still don't work with it), but is there another tool(s) for testing for memory leak?


Yes, as Als has pointed out in a comment and from my personal experience, I would also recommend going with valgrind. There are various options such as --leak-check=yes etc. that you might use. Once you run valgrind, it outputs some recommend options that you can include in the next run.

The problem Valgrind is attempting, i.e., of finding memory leaks, is a complex problem. Sometimes valgrind gets confused and outputs false positives, i.e., it shows a memory leak at a place where there is none. But, other than this, valgrind is quite user-friendly and useful.


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. Use a container to store each memory address that is assigned within new. Remove it from the collection if delete is called. At the end of your program, check if the collection is empty.

Details can be e.g. found in Scott Meyers book Effective C++, Item 50.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号