Where to delete QDrag and QMimeData from startDrag event ( Memory leak )
-
The drag and mime data are never being destroyed, when I drag and cancel the dragging, leading to a memory leak, what event/signal to use to delete the objects?
The following link also does not show how to delete the objects.
https://wiki.qt.io/QList_Drag_and_Drop_ExampleHere is my debugging code:
class MyDrag: public QDrag { public: MyDrag( QObject *parent = 0 ): QDrag( parent ) { qDebug() << "NEW DRAG" ; } ~MyDrag() { qDebug() << "DRAG DESTROYED" ; } };
class MyData: public QMimeData { public: MyData() { qDebug() << "NEW DATA" ; } ~MyData() { qDebug() << "DATA DESTROYED" ; } };
void MyWidget::startDrag(Qt::DropActions supportedActions) { Q_UNUSED( supportedActions ) QMimeData *mimeData = new QMimeData; MyDrag *drag = new MyDrag(this); if( drag->start( Qt::MoveAction) == Qt::MoveAction ){ delete mimeData; delete drag; } }
-
Hi,
Please, use exec, start is an obsoleted function.
Are you sure the move happened ?
-
Hi SGaist,
Yes I am sure there is a move, I drag and drop, and it gets accepted in the graphics scene, and never gets destroyed, even when it gets ignored.
When I use exec, the same thing happens, but I have to remove delete, from the if block, because exec enters the if block directly.
I am thinking about a hack around it, what if I start a single shot timer in the constructor, for one minute, and have it deleteLater the MyDrag and MyData,
No body drags for one minute, right?
-
I just realised, that wiki entry might be a bit outdate. Based on this example, it seems you want to use Qt's item views thus you should rather look at the dedicated drag and drop for item views documentation.
If you want to have your own drag and drop operation on custom widget then Drag And Drop chapter and the accompanying examples are your friends.
-
OK, many thanks, I think I got it.
/// Executing the drag this way will delete it /// when there is drop event. Qt::DropAction dropAction = drag->exec(Qt::CopyAction ); /// Delete later, if there is no drop event. if( dropAction == Qt::IgnoreAction ){ drag->deleteLater(); mimeData->deleteLater(); }