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

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.
  • 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
              • S Offline
                S Offline
                stukdev
                wrote on last edited by
                #21

                If i run valgrind its say so that i've memory lost in
                @
                test = new Test();
                @

                So is correct use delete on descrutor? But i don't understand why my app crash for corrupted double-linked.

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

                  The code looks ok. I don't know what's going wrong here. Can you provide a complete test case?

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

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

                    After hours i found the problem. The problem is a desctructor of QString. I have one class that load some string from a file and store it in a vector (now i can use a QList but this is old code). This vector is used by some class and is copy. In this old project there aren't delete and the program have much memory leak. The code is this:

                    @//Class that load string

                    class Loader
                    {
                    public:
                    Loader();
                    ~Loader();
                    QString* getList();
                    private:
                    QString str[500];
                    };

                    Loader::Loader()
                    {
                    for(quint16 a=0;a<500;a++)
                    str[a] = "a"; //This is example fill str with 'a'
                    }

                    //This is another class that have loader class and call descructor
                    class Test : public QDialog, private Ui::Dprodotti {
                    Q_OBJECT
                    public:
                    Test(QWidget *parent = 0);
                    ~Test();

                    private:
                    Loader *loader;
                    QString myStr[500];

                    private slots:
                    void on_pushButton_clicked();
                    };

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

                    loader = new Loader();
                    memcpy(myStr,product->getList(),sizeof(myStr));
                    
                    setAttribute(Qt::WA_DeleteOnClose);
                    

                    }

                    Test::~Test()
                    {
                    delete loader;
                    }

                    void Test::on_pushButton_clicked()
                    {
                    close();
                    }@

                    There something wrong in that QString vector but i don't understand what...

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

                      Use [[Doc:QStringList]] and get rid of memcopy and all the dangerous pointer stuff. It's not necessary here.

                      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