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. Where to delete QDrag and QMimeData from startDrag event ( Memory leak )
Forum Updated to NodeBB v4.3 + New Features

Where to delete QDrag and QMimeData from startDrag event ( Memory leak )

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.9k 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.
  • T Offline
    T Offline
    Tahar
    wrote on last edited by
    #1

    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_Example

    Here 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;
    
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Please, use exec, start is an obsoleted function.

      Are you sure the move happened ?

      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
      • T Offline
        T Offline
        Tahar
        wrote on last edited by
        #3

        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?

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

          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.

          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
          1
          • T Offline
            T Offline
            Tahar
            wrote on last edited by
            #5

            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();
            }
            
            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