Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] [N00b] Memory management in Qt
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] [N00b] Memory management in Qt

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 10.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    valandil211
    wrote on last edited by
    #1

    Hello Qt devs!

    I just read this passage in C++ GUI Programming with Qt 4 by Jasmin Blanchette and Mark Summerfiedl:

    bq. Qt's parent-child mechanism is implemented in QObject. When we create an object (a widget, validator, or any other kind) with a parent, the parent adds the object tothe list of its children. When the parent is deleted, it walks through its list of children and deletes each child.

    Now, if you take a look at the following code:

    @
    #ifndef EMPLOYEELISTDIALOG_H
    #define EMPLOYEELISTDIALOG_H

    #include <QSqlQueryModel>
    #include "ui_employeelistdialog.h"

    class employeeListDialog : public QDialog, private Ui::employeeListDialog
    {
    Q_OBJECT

    public:
    explicit employeeListDialog(QWidget *parent = 0);

    private:

    // Function that initializes employee data models
    void EMPModels();
    
    // Declares some pointers to help query DB
    // when displaying employee information
    QSqlQueryModel *EMPpr;
    QSqlQueryModel *EMPpa;
    QSqlQueryModel *EMPpapr;
    

    };

    #endif // EMPLOYEELISTDIALOG_H@

    and

    @void employeeListDialog::EMPModels()
    {

    // Initially hides everything in the centralWidget area.
    // Widgets will appear when buttons are triggered.
    // Also sets the tabs' label text
    
    /* Queries the database for the present employees and makes
      a model out of it, so a view can be added later */
    EMPpr = new QSqlQueryModel;
    ...
    ... 
    /* Same thing for past employees */
    EMPpa = new QSqlQueryModel;
    

    ...
    ...

    /* Same thing for all employeese, past and present */
    EMPpapr = new QSqlQueryModel;
    ... 
    ... 
    

    }@

    Do I have to write

    @ EMPpapr = new QSqlQueryModel(this); @

    so that the pointers get deleted, or are they already handled by Qt?

    Also, if I declare some QSqlQuery pointers, which do not specify parents, do I need to delete them manually?

    Joey Dumont

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      Parent / child relationships are either established "explicit" by passing a parent to the constructor / calling QObject::setParent() or "implicit" by objects taking ownership of another eg. adding a widget to a layout. Such situations are usually stated in the documentations.

      But there is no automatism.

      In your case you will have to explicitly set a parent.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chuck Gao
        wrote on last edited by
        #3

        Basiclly, if your instance has parent object(which is QObject base), it will be deleted when parent be deleted. So, firstly, make sure if it's QObject-based. If the answer is not, you can delete the pointer in the deconstructor. Or use smart pointer in Qt.

        Chuck

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chuck Gao
          wrote on last edited by
          #4

          If you use QML with Qt C++, it's more complicated. But the core concept you need know is ownership. Also in QtScript. You can also think about if the pointer be deleted twice, and what will happen in Qt.

          Chuck

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valandil211
            wrote on last edited by
            #5

            Okay.

            For instance, when I close a dialog using, say

            @connect(EMP_closeButton, SIGNAL(clicked()), this, SLOT(close()));@

            will this call the destructor, or will it just hide the dialog?

            Do I need to set Qt::WA_DeleteOnClose? If yes, where do I do that?

            Thanks for your incredibly fast reponses, guys!

            Joey Dumont

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Yes, you should give your QSqlQueryModel instances an explicit parent. If you do not specify a parent, and none is set automatically (for instance inserting a widget into a layout of a parent widget will take care of parent-child relationships automatically), you need to take care of deleting any object you created on the heap yourself. That probably come down to calling delete on the pointers in the destructor of the class you created them in. Or, as Chuck.Gao suggests, use smart pointers to take care of that for you.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                valandil211
                wrote on last edited by
                #7

                Okay. So since everything (dialogs, widgets...) I call from the MainWindow has the MainWindow as its parent, the delete process is taken care of when I close the window using the code above. Where do I set Qt::DeleteOnClose?

                What happens if I delete a pointer twice?

                Thanks,

                Joey Dumont

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  close() won't delete the dialog (= call the destructor) unless the WA_DeleteOnClose attribute is set. The flag can be set any time (preferebly before closing) using QWidget::setAttribute, and - of course - if your dialog is created on the heap (if it is created on the stack it will be destructed automatically when going out of scope).

                  However, if you set a parent for your dialog, it will be automatically deleted as soon as the parent is deleted. If you do not set a parent, it is your responsibility to delete the dialog (by either calling delete or setting the WA_DeleteOnClose attribute).

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    valandil211
                    wrote on last edited by
                    #9

                    Thanks for answers, Qt devs! I really appreciate it.

                    Joey Dumont

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #10

                      [quote author="Joey Dumont" date="1309184687"]What happens if I delete a pointer twice? [/quote]

                      If the pointer has been set to 0 after the first deletion usually nothing, as @
                      delete 0;
                      @ is perfectly valid C++ if I remember correctly.

                      However, if you call delete on a non-null pointer which has already been deleted this usually leads to unexpected behaviour and will crash your application at best.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        valandil211
                        wrote on last edited by
                        #11

                        In that case, I'll be careful to always declare a parent. Seems like the easiest solution.

                        Thanks again!

                        Joey Dumont

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mlong
                          wrote on last edited by
                          #12

                          bq. In that case, I’ll be careful to always declare a parent. Seems like the easiest solution.

                          And, if you do ever delete anything manually, be sure to set the pointer to 0 after the call to delete. It's a good, safe practice.

                          Software Engineer
                          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved