简体   繁体   中英

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,

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?

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, ie, of finding memory leaks, is a complex problem. Sometimes valgrind gets confused and outputs false positives, ie, 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 eg found in Scott Meyers book Effective C++, Item 50.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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