softghost
softghost

Reputation: 1197

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?

Upvotes: 1

Views: 1099

Answers (2)

amit kumar
amit kumar

Reputation: 21012

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.

Upvotes: 1

Reinhard
Reinhard

Reputation: 399

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.

Upvotes: 0

Related Questions