Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Destructor array of object
QtWS25 Last Chance

Destructor array of object

Scheduled Pinned Locked Moved C++ Gurus
24 Posts 5 Posters 14.1k Views
  • 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.
  • S Offline
    S Offline
    stukdev
    wrote on last edited by
    #1

    Hi, i have this array

    @
    QRadioButton *radio[8];
    for(quint8 a=0;a<8;a++)
    radio[a] = new QRadioButton();
    @

    What is the best practise to delete all item in the descructor class?

    @
    delete [] radio;
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      That leads to disaster!

      Your array is allocated on the stack and will be deleted automatically once it goes out of scope. calling delete[] on it will eventually destroy the array twice, so you must not do that!

      If you need to delete the array manually, you must allocate it manually too:

      @
      QRadioButton* radio[] = new QRadioButton* [8];
      // ...
      delete [] radio;
      @

      In both cases, that deletes the array only, but not the pushbuttons contained in the array! In fact delete[] destroys what's contained in the array, and that is not radio buttons, but pointers to QRadioButtons! So only the pointer is deleted, but ont the object it is pointing to.

      You will have to iterate through the array and delete all the buttons manually, much as like as you created them:

      @
      for(int i = 0; i < 8; ++i)
      delete radio[i];
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

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

        Or, if the QRadioButtons have a parent, for example because they have been placed inside a layout, the buttons get deleted automatically as soon as the parent is deleted. However, you still have to delete the array itself (when allocated on the heap).

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stukdev
          wrote on last edited by
          #4

          Thanks for reply. In real case my array is a private member of the class so is not allocated on the stack.

          I try this in descructor

          @
          for(quint8 a=0;a<8;a++)
          delete radio[a];
          delete [] radio;
          @

          but i got a crash. When i create object i use parent like this

          @
          radio[a] = new QRadioButton(parentWidget);
          @

          This can create problems?

          1 Reply Last reply
          0
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #5
            1. Since your buttons have a parent, you don't have to delete them.
            2. Your array is a regular one (unless you implemented it differently from your 1st snippet) so DON'T delete it.

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              A member of a class (be it public, protected or private doesn't matter at all) can be allocated on the stack or on the heap.

              @
              class blurb {
              public:
              blurb() {
              radioHeap = new QRadioButton *[8];
              }

              ~blurb() {
                  delete[] radioHeap;
                  // never ever call this:
                  // delete[] radioStack;
              }
              

              private:
              // an array of size 8 allocated on the stack
              // DO NOT call delete[] in the destructor
              QRadioButton *radioStack[8];

              // an array of unknown size allocated on the heap
              // you have to allocate it somewhere (eg. constructor)
              // and have to delete it somewhere (eg. destructor)
              QRadioButton *radioHeap[];
              

              };
              @

              So, if you defined it like in the first post, Johan is right and you do a double deletion.

              BTW: Why don't you use one of the nice container classes of Qt? One should avoid raw arrays unless absolutely needed. They're cumbersome to use.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                stukdev
                wrote on last edited by
                #7

                Ooh i don't know the difference from
                @
                QRadioButton *radioStack[8];
                //and
                QRadioButton *radioHeap[];
                @
                Thank's.

                [quote author="Volker" date="1329155246"]
                BTW: Why don't you use one of the nice container classes of Qt? One should avoid raw arrays unless absolutely needed. They're cumbersome to use.[/quote]

                What type of container class can i use here?

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KA51O
                  wrote on last edited by
                  #8

                  Here's an overview for all of "Qt's container classes":http://developer.qt.nokia.com/doc/qt-4.8/containers.html. Some are optimized for quick access, some for quick insertion...

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    stukdev
                    wrote on last edited by
                    #9

                    So i can get this:

                    @
                    //private definition class
                    private:
                    QList<QRadioButton*>radio;

                    //costructor class
                    for(quint8 a=0;a<8;a++)
                    radio << new QRadioButton(parentWidget);
                    @

                    This is more efficient then create manually array and i haven't to delete anything in descructor. Right?

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      KA51O
                      wrote on last edited by
                      #10

                      correct

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        [quote author="stuk" date="1329208992"]So i can get this:

                        @
                        //private definition class
                        private:
                        QList<QRadioButton*>radio;

                        //costructor class
                        for(quint8 a=0;a<8;a++)
                        radio << new QRadioButton(parentWidget);
                        @

                        This is more efficient then create manually array and i haven't to delete anything in descructor. Right?[/quote]

                        NO

                        Regarding efficiency:
                        Using a container instead of a plain old array is always a bit slower, but for the common use cases this plain doesn't matter because no one ever notices. And advantages, like consistent API and safety, prevail massively most times.

                        Regarding deletion:
                        The container classes, if filled with pointers, never call delete on that pointers once they are removed from the list or in case the list is emptied or destroyed. You must call delete yourself. In case of the Qt container classes there is a nice helper function:

                        @
                        qDeleteAll(radio);
                        @

                        http://www.catb.org/~esr/faqs/smart-questions.html

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

                          [quote author="Volker" date="1329217409"]
                          The container classes, if filled with pointers, never call delete on that pointers once they are removed from the list or in case the list is emptied or destroyed. You must call delete yourself.[/quote]

                          Well, generally speaking this is true.

                          [quote author="stuk" date="1329208992"]... i haven't to delete anything in destructor.[/quote]

                          But one should add that in this particular case you probably need not to delete anything in the destructor, because the QRadioButton objects are automatically deleted as soon as parentWidget is deleted. So as long as parentWidget is deleted somewhen there is no need to call delete on the objects - they are deleted automatically due to Qts parent/child system.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #13

                            Yes, that's true. Thanks for adding that clarification, Lukas. I was too focused on the container classes themselves.

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              stukdev
                              wrote on last edited by
                              #14

                              This topic become very interesting. Another question.
                              If i use QList and my radio button have parents but i set for my dialog a flag
                              @
                              setAttribute(Qt::WA_DeleteOnClose,true);
                              @
                              If i close the dialog radio buttons are deleted or not?
                              The parents is a QMainWindows so the parent is alive!

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                KA51O
                                wrote on last edited by
                                #15

                                if the parent of the radiobuttons is the dialog, the radiobuttons are deleted when the dialog is closed.
                                If the parent of the radiobuttons is the mainwindow but the buttons are only displayed in the dialog the buttons are finally deleted when the mainwindow is closed I think. I'm not sure about the effect of the attribute you set.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  stukdev
                                  wrote on last edited by
                                  #16

                                  I have a QMainWindow that open more dialog. I want create the dialog on a click and delete all its objects on a close button of that dialog for optimize memory.

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    KA51O
                                    wrote on last edited by
                                    #17

                                    If you set the parent for all the elements displayed in the dialog correctly Qt will take care of the destruction of all the children of the dialog. So if your radiobuttons are displayed in the dialog and the dialog is set to be their parent you don't have to do anything with regard to destroying your buttons.

                                    But if you want to reuse the buttons in another dialog you can of course set the mainwindow as their parent. This way you only need to create them once, store the pointers to them in your list and put them in every dialog you want. They should only finally be deleted when your mainwindow is closed. I haven't tried this myself so no guarantees that this is working.

                                    Heres a "link to a thread":http://developer.qt.nokia.com/forums/viewthread/6646 which might also interest you.

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      goetz
                                      wrote on last edited by
                                      #18

                                      Be aware: Once you put the radio buttons into a layout, the parentship of the the buttons is transferred to the layout's widget. So, if you create the buttons with QMainWindow as parent and put them into the layout contained somewhere in an independent QDialog, then the buttons are finally owned by that dialog or one of it's subwidgets!

                                      http://www.catb.org/~esr/faqs/smart-questions.html

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        stukdev
                                        wrote on last edited by
                                        #19

                                        So i have this situation. Two class, one have a pointer to another class.

                                        @
                                        //Class base
                                        class Base : public QDialog, private Ui::Base {
                                        Q_OBJECT
                                        public:
                                        Base(QWidget *parent = 0);
                                        ~Base();
                                        private:
                                        Test *test;
                                        };

                                        Base::Base(QWidget *parent) : QDialog(parent)
                                        {
                                        setupUi(this);

                                        test = new Test();
                                        
                                        //Delete object after close
                                        setAttribute(Qt::WA_DeleteOnClose);
                                        

                                        }

                                        Base::~Base()
                                        {
                                        //delete test //this crash if uncomment
                                        }

                                        //Test class
                                        class Test
                                        {
                                        public:
                                        Test();
                                        ~Test();
                                        };

                                        Test::Test()
                                        {}

                                        Test::~Test()
                                        {
                                        qDebug() << "death";
                                        }
                                        @

                                        If i uncomment the line

                                        @
                                        delete test //this crash if uncomment
                                        @

                                        I see death in console but my app crash, otherwise if i comment the program don't crash but i don't see death and in top manager the memory is not free. What's the problem?

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          goetz
                                          wrote on last edited by
                                          #20

                                          Run in a debugger and look where the crash occurs.

                                          http://www.catb.org/~esr/faqs/smart-questions.html

                                          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