I just answered this question and wanted to provide a working example when I noticed that the newly created QMimeData instance returned by QListModel::mimeData() will not be deleted until the application is completed.
Thus, this is not a real memory leak, since Qt processes all instances of QMimeData at shutdown, but you only need to drag and drop and just drag and drop the desired content into your mime data to make the memory fully work.
Did I miss something? Is there any way to tell Qt to remove instances of QMimeData as soon as they are no longer needed?
Note:
I know that every instance of QMimeData automatically deleted by Qt when the program terminates. My problem here is not the actual memory leak reported by valgrind or cppcheck , but it seems that multiple instances and potentially very large instances of QMimeData not cleared at runtime, which also increases memory consumption.
Code example:
#include <QtWidgets> #include <iostream> struct TrackedMimeData : public QMimeData { TrackedMimeData(const QString & text) { std::cout << this << std::endl; setText(text); } ~TrackedMimeData() { std::cout << "~" << this << std::endl; } }; struct MyListWidget : QListWidget { MyListWidget() { setDragEnabled(true); addItem("item1"); addItem("item2"); } QMimeData * mimeData(const QList<QListWidgetItem *>) const override { return new TrackedMimeData("hello"); } }; int main(int argsc, char *argsv[]) { QApplication application(argsc, argsv); MyListWidget gui; gui.show(); return application.exec(); }
An example output is as follows:
0xa58750 0xa4e0f0 ~0xa4e0f0 0xa3c6c0 ~0xa3c6c0 0xa51880 0xa5ecd0 0xa31f50 0xa57db0 0xa5afc0 ~0xa5afc0 0xa5aa70 ~0xa5aa70 ------ CLOSE WINDOW ~0xa58750 ~0xa51880 ~0xa5ecd0 ~0xa31f50 ~0xa57db0
Destructors are called before closing the application only when drop is accepted.
Btw. I am on home Qt 5.6 @ 1fcdb6cafcf - on one computer and on 5.6.0-19.fc23 Fedora 23 is precompiled on another. Therefore, I doubt that this is only a temporary state of development.
frans source share