Memory Management in Qt/C++
-
Hello,
I am currently developing a GUI front-end application for DVR(Digital Video Recording). After completing the coding part of it, I have tested it many times and it is working fine but I want to it to be more robust. For that I looked into memory management part of it. Then I have tested my code binary with the valgrind tool which is the ultimate memory leak tester. I found 63 errors in my code after testing with valgrind. I have went thorough output of the valgrind but not able to understand how it was working and telling actually where is the error. I am totally confused. Do not know from where to start.
I have also studied about some memory management technique likes of manually deletion of the object which we have created using "delete <object_name>" command but that is not helping me.
Please guide me because this is my first production level program and I do not have any knowledge about memory management in qt. So please help me.
-
There are some good tutorials for Valgrind. Take a look to
"CProgramming / Debug / Valgrind":http://www.cprogramming.com/debugging/valgrind.html
"Valgrind: Quick-Start":http://valgrind.org/docs/manual/quick-start.html
"Valgrind: FAQ":http://valgrind.org/docs/manual/faq.html -
An other good article:
"Linuxprogrammingblog":http://www.linuxprogrammingblog.com/using-valgrind-to-debug-memory-leaks
-
Is use of smart pointers will be useful for memory management ?
-
Hi Gwal Ashish,
smart pointer help with memory management if they are managing smart pointers (not like QPointer shich keeps track if the object behind exists).
In QObject hierarchies (parent / child) all children will automatically be deleted, so no need of smart pointers here, but the top level elements must be deleted manually.
-
[quote author="Gerolf" date="1355130051"]Hi Gwal Ashish,
smart pointer help with memory management if they are managing smart pointers (not like QPointer shich keeps track if the object behind exists).
In QObject hierarchies (parent / child) all children will automatically be deleted, so no need of smart pointers here, but the top level elements must be deleted manually.[/quote]
Hi Gerolf,
I have understanding about QObject hierarchies but as you suggested in your post that I have to delete top level element manually then what approach should be useful in our thinking. Please suggest me something to start with then I will try to implement.
-
[quote author="Gerolf" date="1355228848"]e.g. in Main, you can create the widgets on the stack.
If you have top level QObjects elsewhere where the stack is not possible, use smart pointers like shared pointers or others.[/quote]Hi Gerolf,
Will you please give me some example related this to study because everything which is related to memory leak is going over my head. So it would be nice to have some example to go through and then decide how to about memory leaks.