c++qtmemory-leaksvisual-leak-detector

QListWidget memory leak


So I have this method:

void SomeClass::SomeMethod() {
   QString information;
   QListWidgetItem *dataline = new QListWidgetItem();

   information = "stuff & more stuff";

   dataline->setText(information);
   dataline->setFont(QFont("Monospace"));
   dataline->setStuff(moreStuff::things);

   //! listData is a pointer to QListWidget object.
   ui.listData->insertItem(ui.listData.count(), dataline);
} //! Boom, memory leak!

In Qt docs it says something like this:

If you need to insert a new item into the list at a particular position, then it should be constructed without a parent widget. The insertItem() function should then be used to place it within the list. The list widget will take ownership of the item.

But VLD (Visual Leak Detector) detects memory leak every time I call that method.

1) Is VLD actually right about this, and there is a memory leak?

2) Is there another way of solving this? (I tried not to heap-allocate the dataline object, and pass the address of it to the insertItem method, but it seems that is not working.

EDIT: This is the class that holds the ui object:

#include "ui_Validator.h"

class Validator: public QMainWindow {
        Q_OBJECT
    public:
        Validator(QWidget *parent = 0);
        ~Validator();
    public slots:
        void stuff1();
        void stuff2();
        void stuff3();
        void stuff4(const QString &data);
        void stuff5(const Rats &data);
    signals:
        void startGeneratingSignal(Cat param);
        void stopGeneratingSignal(Dog param);
        void initStuff();
        void configStuff(Rats param);
        void stopThreadIOloop();
    private:
        Ui::Validator ui; //! This object is from Qt-generated ui_Validator.h.
        SomeThreadObj *objActivity;
        QThread *objActivity;
        SomeInterface interface;
    };

And the destructor from cpp file:

Validator::~Validator() {
    //! EDIT: It's ok, when this destructor is called, it means that all the application is closed, so there is no need for explicit cleanup, the OS will take care of that.
    ui.listData->clear();
    emit killIOloop();
    qDebug() << "~Validator";
}

So, like you can see, the ui object is constructed first time when the program starts (the main window), and goes out of scope when the destructor is called (when I exit the application) (for some reasons I intentionally don't delete objActivity and objActivity).


Solution

  • I have written a simple example with VLD and the code below:

    test1::test1(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        QString information;
        QListWidgetItem *dataline = new QListWidgetItem();
    
        information = "stuff & more stuff";
    
        //Dataline dataline = new Dataline();
        dataline->setText("test");
        dataline->setFont(QFont("Monospace"));
    
    
        //! listData is a pointer to QListWidget object.
        ui.listData->insertItem(ui.listData->count(), dataline);
    }
    
    test1::~test1()
    {
    
    }
    

    After the MainWindow is closed, QListModel Destructor is called and deletes all items of the ListWidget in a clear Method.

    Output of VLD:

    No memory leaks detected.
    Visual Leak Detector is now exiting.
    

    If you call the clear Method of the ListWidget explicit: Same Output.