Results 1 to 3 of 3

Thread: Memory management

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Memory management

    Hi everybody,

    I have a little question about memory management.
    When you construct a QDialog you pass the parent object into the child object. So the child is released when you release the parent object.

    In my current project I have a QMainWindow, a some QDialogs. Each time a person wants to add a order a QDialog is allocated, the QDialog is the parent of the orderRecord class which is deallocated perfectly when I destruct my QDialog. Only my QDialog remains in the memory. So when you're opening let's say the window a 1000 times in one day (although that's excessive) the memory amount keeps on growing.

    I will explain how it works, when I'm creating a QDialog I'am creating a class with a pointer.
    Qt Code:
    1. void MainWindow::openOrderAddWindow()
    2. {
    3. qDebug("open order add dialog");
    4.  
    5. OrderAddWindow *window = new OrderAddWindow(this);
    6.  
    7. Query *query = new Query(database->getConnection(), this);
    8.  
    9. //connect the save data signal to the add slot from query.
    10. //add will add the record data returned by saveData into the database
    11. connect(window, SIGNAL(saveData(OrderRecord&)),
    12. query, SLOT(add(OrderRecord&)));
    13. connect(query, SIGNAL(querySucceeded(bool)),
    14. window, SLOT(queryResponse(bool)));
    15. connect(window, SIGNAL(dealloc(OrderAddWindow*)),
    16. this, SLOT(deallocOrderAddWindow(OrderAddWindow*)));
    17. }
    To copy to clipboard, switch view to plain text mode 

    When the queryResponse signal is true, I know that everything went fine, so I can deallocate the orderRecord object which the query class uses to get data from. And I can destroy the QDialog.

    Qt Code:
    1. void OrderAddWindow::queryResponse(bool succeeded)
    2. {
    3. qDebug("OrderAddWindw::queryResponse(): The data is saved in the database, so we can close the window");
    4. //close window
    5. close();
    6. //destruct parent and childs
    7. destroy(true, true);
    8.  
    9. this->startDeallocating();
    10. }
    To copy to clipboard, switch view to plain text mode 

    So I uses the protected function destroy(true, true) which will deallocate QDialog and all his childs.


    The image monitors the memory allocation. The second run is with destroy called.
    The first run is when start Deallocating() is called which emits the signal dealloc, this signal is connected to a function in QMainWindow which will delete the QDialog.

    Qt Code:
    1. void MainWindow::deallocOrderAddWindow(OrderAddWindow *window)
    2. {
    3. qDebug("MainWindow::deallocOrderAddWindow: Starting deallocating procedure");
    4. //this->destroy(false, true);
    5. delete window;
    6. }
    To copy to clipboard, switch view to plain text mode 

    So as you can see, in both runs the memory keeps on growing..... how can I change that, or is it normal?

    Greetings,

    Cyberboy

  2. #2
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Memory management

    I have some kind of solution, I delete the objects manually.
    So I use the delete function of c++, is this the right way to solve this problem?...

    greetings cyberboy

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Memory management

    When you close a window, it is not being destroyed. You either should create the dialog on the stack and use QDialog::exec() to make it modal, delete the dialog manually or set the Qt::WA_DeleteOnClose attribute on the dialog so that it gets deleted after being closed.

Similar Threads

  1. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  2. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  3. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  4. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39
  5. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21

Bookmarks

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.