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. Deleting an object on the heap that is stored in QList<T>
Forum Updated to NodeBB v4.3 + New Features

Deleting an object on the heap that is stored in QList<T>

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 3.7k 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.
  • P Offline
    P Offline
    pongo
    wrote on last edited by
    #1

    Hi,
    I'm stuck on this one:
    i have a QList<Node> that stores items like this:
    @nodeList.append(*(new Node(...));@
    i chose not to make it a pointer since QList stores it anyway as a pointer

    in the destructor i want to clear the memory taken by all the Nodes.

    on the documentation of QList i found this
    @QList<QWidget *> list;
    ...
    while (!list.isEmpty())
    delete list.takeFirst();@
    but that doesn't really help me since i got QList<T> list

    would be thankfull for any advices
    thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Just a quick question, since your are storing Node object why do you use new ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pongo
        wrote on last edited by
        #3

        hi,
        thanks for the quick reply

        i'v got:
        @
        class WoodyStructure : public QObject
        {
        Q_OBJECT
        public:
        explicit WoodyStructure(QObject parent = 0);
        Node
        deleteAllNodes();
        ~WoodyStructure();

        inline int getNONodes() const {return nodeList.count();}
        inline const Node& getNode(int index) const { return nodeList.at(index);}
        

        //some setters
        inline void setNode(int index, Node &aNode) {nodeList[index] = aNode;}
        //..
        private:
        QList<Node> nodeList;
        QList<Node> beamList;
        QList<Node> materialList;
        QList<Node> sectionList;
        // and much more stuff like this
        @

        this is my main class that stores all my data and i want to be able to create a note anywhere in the programm since the gui looks something like
        @
        #include "woodystructure.h"
        #include "woodysettings.h"

        namespace Ui {
        class WoodyMainWindow;
        }

        class WoodyMainWindow : public QMainWindow
        {
        Q_OBJECT

        public:
        //Constructor
        explicit WoodyMainWindow(QWidget *parent = 0);
        ~WoodyMainWindow();
        //Constructor END

        void initWoodyStructure();
        

        private:

        //probably public....
        inline void readSettings()  {WoodySettings set; set.readSettings();}
        inline void writeDefaultSettings()   {WoodySettings set;set.writeDefaultSettings();}
        inline void changeSetting(QString key,QString value) {WoodySettings set; if(!set.contains(key)){qDebug("tried to change not existant setting"); } else set.changeSetting(key,value);}
        

        private slots:
        void on_submit_PB_clicked();
        void on_commandLine_returnPressed();

        private:
        Ui::WoodyMainWindow *ui;
        WoodyStructure *Ws;
        // and a lot of other QGui Stuff

        };@
        so i can acces everything within "Ws."

        thanks in advance

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Thank you for the detail but my question was why:

          @nodeList.append(*(new Node(...));@

          rather than

          @nodeList.append(Node(...));@ ?

          And for your question: the Nodes will be destroyed when the list goes out of scope.

          If you were to change your mind and store pointers you can use "this":http://qt-project.org/doc/qt-4.8/qtalgorithms.html#qDeleteAll

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #5

            I think SGaist is already on to this, but just for clarity:
            @nodeList.append(*(new Node(...));@
            usually is a memory leak. You create a node on the heap, dereference it and store it by value (i.e. a copy of it) in the nodeList. The original object is still in memory, but its pointer is lost as soon as the append() statement is processed. Yes, the QList works with pointers internally, but in this case it stores a pointer to a copy of your object.

            If you have a QList<T> and your T properly defines copy-ctor, dtor and assignment operator to support value semantics, you can just clear the list to free up memory (QList::clear). Note that the copy-by-value which happens upon QList::append allocates a T on the heap, managed by QList. So just removing it from the list also frees the T's memory.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #6

              Hi, I wonder how did you get to this conclusion?

              [quote author="pongo" date="1375284738"]
              i chose not to make it a pointer since QList stores it anyway as a pointer[/quote]

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pongo
                wrote on last edited by
                #7

                thanks guys for the detailed explanation. i guess i will try to modify the architecture that it will be possible to work with @nodeList.append(Node(...));@

                like SGaist said. that way i'm saver concerning memory leaks :-)

                bq. Robot Herder wrote:
                Hi, I wonder how did you get to this conclusion?

                actually, i'm quite new to Qt and even to C++ and in the begining i was going crazy with pointers so a friend told me this solution, but i think it's possible i got it wrong...

                thanks again,

                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