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



  • 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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • 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


  • Lifetime Qt Champion

    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



  • 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.



  • 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]



  • 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,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.