Memory leak with valgrind
-
Hi!
I know that there are a lot of post that talk about memory leaks and problems with valgrind. This is another one; I don't found a solution to my problems.
I found somewhere that valgrind isn't qt-friendly (like gtk-friendly), somewhere I found that valgrind need to be run with some parameters...
I have implemented 4 codes, and everyone take me some errors.
First of all:
@#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}@
this one take that valgrind output:
@==16463== HEAP SUMMARY:
==16463== in use at exit: 1,835,247 bytes in 10,735 blocks
==16463== total heap usage: 91,708 allocs, 80,973 frees, 7,208,968 bytes allocated
==16463==
==16463== LEAK SUMMARY:
==16463== definitely lost: 5,716 bytes in 16 blocks
==16463== indirectly lost: 358,950 bytes in 1,396 blocks
==16463== possibly lost: 429,301 bytes in 1,866 blocks
==16463== still reachable: 1,041,280 bytes in 7,457 blocks
==16463== suppressed: 0 bytes in 0 blocks
==16463== Rerun with --leak-check=full to see details of leaked memory
==16463==
==16463== For counts of detected and suppressed errors, rerun with: -v
==16463== Use --track-origins=yes to see where uninitialised values come fro@Second one, that try to delete the label pointer:
@ #include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
int ret = app.exec();
delete label;
return ret;
}
@
output is:
@==9916== HEAP SUMMARY:
==9916== in use at exit: 1,828,885 bytes in 10,684 blocks
==9916== total heap usage: 94,660 allocs, 83,976 frees, 7,427,884 bytes allocated
==9916==
==9916== LEAK SUMMARY:
==9916== definitely lost: 5,676 bytes in 15 blocks
==9916== indirectly lost: 352,636 bytes in 1,346 blocks
==9916== possibly lost: 425,007 bytes in 1,805 blocks
==9916== still reachable: 1,045,566 bytes in 7,518 blocks
==9916== suppressed: 0 bytes in 0 blocks
==9916== Rerun with --leak-check=full to see details of leaked memory
==9916==
==9916== For counts of detected and suppressed errors, rerun with: -v
==9916== Use --track-origins=yes to see where uninitialised values come from
==9916== ERROR SUMMARY: 31 errors from 9 contexts (suppressed: 13 from 7)@Another program that subclass QDialog to reimplement the destructor:
@ #include <QApplication>
#include <QtGui>class MyDialog: public QDialog
{
QLabel * label;
public:
MyDialog():QDialog()
{
label = new QLabel("Hello Qt!", this);
setWindowTitle("Dialog");
resize(200,200);
}
~MyDialog()
{
delete label;
}
};int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyDialog dia;
dia.show();
return app.exec();
}@and the output:
@==18639== HEAP SUMMARY:
==18639== in use at exit: 1,828,900 bytes in 10,685 blocks
==18639== total heap usage: 91,822 allocs, 81,137 frees, 7,222,844 bytes allocated
==18639==
==18639== LEAK SUMMARY:
==18639== definitely lost: 5,676 bytes in 15 blocks
==18639== indirectly lost: 352,636 bytes in 1,346 blocks
==18639== possibly lost: 429,791 bytes in 1,873 blocks
==18639== still reachable: 1,040,797 bytes in 7,451 blocks
==18639== suppressed: 0 bytes in 0 blocks
==18639== Rerun with --leak-check=full to see details of leaked memory
==18639==
==18639== For counts of detected and suppressed errors, rerun with: -v
==18639== Use --track-origins=yes to see where uninitialised values come from
==18639== ERROR SUMMARY: 31 errors from 9 contexts (suppressed: 13 from 7)@the last one program, that only use pointers:
@#include <QtGui>int main( int argc, char** argv)
{
QApplication *app = new QApplication( argc, argv);
QDialog *main = new QDialog();
QHBoxLayout *layout = new QHBoxLayout();
QLabel *label = new QLabel("HOla");
layout->addWidget( label );
main->setLayout( layout );main->show();
int ret = main->exec();
delete label;
delete layout;
delete main;
delete app;return ret;
}@
the output is:
@==5540== HEAP SUMMARY:
==5540== in use at exit: 1,828,236 bytes in 10,680 blocks
==5540== total heap usage: 93,713 allocs, 83,033 frees, 7,335,768 bytes allocated
==5540==
==5540== LEAK SUMMARY:
==5540== definitely lost: 5,676 bytes in 15 blocks
==5540== indirectly lost: 351,982 bytes in 1,342 blocks
==5540== possibly lost: 429,791 bytes in 1,873 blocks
==5540== still reachable: 1,040,787 bytes in 7,450 blocks
==5540== suppressed: 0 bytes in 0 blocks
==5540== Rerun with --leak-check=full to see details of leaked memory
==5540==
==5540== For counts of detected and suppressed errors, rerun with: -v
==5540== Use --track-origins=yes to see where uninitialised values come from
==5540== ERROR SUMMARY: 31 errors from 9 contexts (suppressed: 13 from 7)@With this last code, I try to take out the 4 delete statement, and valgrind output the same number of definitely lost blocks
Could anyone tell me what's the correct form to test memory leaks, or avoid them.
Thanks
-
Did you "search":http://developer.qt.nokia.com/search/tag/valgrind the forum before asking?
-
Yes (provably not all, but a lot of them), and I try some solutions proposed and they couldn't solve my problem