Results 1 to 4 of 4

Thread: Best way to implement the MyModel::mimeData() method without causing a memory leak?

  1. #1
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2

    Default Best way to implement the MyModel::mimeData() method without causing a memory leak?

    Hello Qt Community,

    I'm wondering, what is the best way to implement the mimeData() method in my custom model class (inheriting QAbstractItemModel) without causing a memory leak.
    If I write somethink like this
    Qt Code:
    1. QMimeData* MyModel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. return new QMimeData(some stuff here);
    4. }
    To copy to clipboard, switch view to plain text mode 
    then it is not clear, who should free the allocated memory.

    The original implementation of QAbstractItemModel also allocates new memory without dealocating it :
    Qt Code:
    1. QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
    2. {
    3. if (indexes.count() <= 0)
    4. return 0;
    5. QStringList types = mimeTypes();
    6. if (types.isEmpty())
    7. return 0;
    8. QMimeData *data = new QMimeData();
    9. QString format = types.at(0);
    10. QByteArray encoded;
    11. QDataStream stream(&encoded, QIODevice::WriteOnly);
    12. encodeData(indexes, stream);
    13. data->setData(format, encoded);
    14. return data;
    15. }
    To copy to clipboard, switch view to plain text mode 

    Does anybody already encountered a problem or have any suggestions?

    Thanx in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to implement the MyModel::mimeData() method without causing a memory lea

    The convention of this method, as evident by the base class's implementation, assumes transfer of ownership to the caller.
    Any caller not taking care of deleting the now owned object is in error.

    If you model is long lived, e.g. lives for the whole duration of the program's existance, then you could theoretically set the model as the QMimeData object's parent.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Yevgen (6th March 2014)

  4. #3
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2

    Default Re: Best way to implement the MyModel::mimeData() method without causing a memory lea

    I've tried your proposal before by setting the model as the parent of the new QMimeData object. And I explicitely didn't delete the pointer anywhere in code manually. Surpisingly it resulted in crash, when the application gets closed. So, somebody deletes the pointer.

    The drawback of the convention with an ownership transfer for me is when the dragged object leaves the application scope and is dropped somewhere else.

    Thank you for the answer.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to implement the MyModel::mimeData() method without causing a memory lea

    Well, the object, as in the instance of QMimeData, obviously never leaves the scope of the process.

    Of course a shared pointer would be nice but such API changes can't be introduced that easily.

    Cheers,
    _

Similar Threads

  1. memory leak in qml
    By jindoniit in forum Qt Quick
    Replies: 6
    Last Post: 26th September 2011, 10:16
  2. When I use Listview with icons its causing memory leak
    By GuusDavidson in forum Qt Programming
    Replies: 4
    Last Post: 29th March 2011, 22:08
  3. implement 'nextSibling()' method
    By mickey in forum General Programming
    Replies: 1
    Last Post: 18th July 2010, 07:47
  4. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 16:24
  5. Qt 4.1 Memory Leak
    By blackliteon in forum Qt Programming
    Replies: 14
    Last Post: 10th February 2006, 12:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.