Results 1 to 2 of 2

Thread: Memory leaks!

  1. #1
    Join Date
    Jan 2011
    Qt products
    Qt4 Qt/Embedded
    Windows Symbian S60

    Default Memory leaks!

    I have such a code and I wonder how should I clean up after it? Should I delete all elements one by one? Should I delete also layouts? Or can I delete just the main element - parent of every other and hope that he will clean up the rest of them?

    Qt Code:
    1. TableTab::TableTab(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4. objectNameLabel_ = new QLabel;
    5. personLabel_ = new QLabel;
    6. tableWidget_ = new RequestTable(parent);
    7. summaryBox_ = new QGroupBox;
    8. commentaryTextEdit_ = new QPlainTextEdit;
    9. normatywLabel_ = new QLabel;
    10. sumLabel_ = new QLabel;
    11. reInvoiceLabel_ = new QLabel;
    12. QGroupBox *calculationsBox = new QGroupBox;
    14. objectNameLabel_->setText("OBIEKT 1");
    15. personLabel_->setText("JOLA");
    16. normatywLabel_->setText("666");
    17. sumLabel_->setText("667");
    18. reInvoiceLabel_->setText("668");
    20. QBoxLayout *layout = new QVBoxLayout;
    21. layout->addWidget(normatywLabel_);
    22. layout->addWidget(sumLabel_);
    23. layout->addWidget(reInvoiceLabel_);
    24. calculationsBox->setLayout(layout);
    26. layout = new QHBoxLayout;
    27. layout->addWidget(commentaryTextEdit_);
    28. layout->addWidget(calculationsBox);
    29. summaryBox_->setLayout(layout);
    31. layout = new QVBoxLayout;
    32. layout->addWidget(objectNameLabel_);
    33. layout->addWidget(personLabel_);
    34. layout->addWidget(tableWidget_);
    35. layout->addWidget(summaryBox_);
    36. setLayout(layout);
    37. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Thanked 260 Times in 246 Posts
    Qt products
    MacOS X Unix/X11 Windows Android

    Default Re: Memory leaks!

    You don't need to delete any of those, you just need to make sure that the class instance (that's the parent of everything in there) is deleted (either by allocating on stack or by setting a mainwindow parent that is allocated on stack or explicitly deleted if allocated on heap and it doesn't have another parent), so basically you need to delete only the QObjects that don't have a parent - those objects when deleted will delete their children. //of course for the classes that doesn't take part in QObject's parent-child relation-ship the standard C++ rules apply.

    Anyway you can read more about parent-children in the documentation here

Similar Threads

  1. QT Memory Leaks
    By lukabratzi in forum Qt Programming
    Replies: 17
    Last Post: 28th February 2012, 21:01
  2. Memory Leaks
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2008, 17:26
  3. Memory leaks..
    By santhoshv84 in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2008, 20:28
  4. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 09:00
  5. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 10:55

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.