Results 1 to 2 of 2

Thread: Qt 4.6: Which Qt smart pointer to use for this case?

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt 4.6: Which Qt smart pointer to use for this case?

    I'm storing "new" objects into a QList, and was wondering which Qt smart pointer should be used for it. Obviously, the memory leak way is to just store the plain-old pointer into the QList (and have my program leaks memory).

    I think the functional requirement I want is that when the QList destructs, I want all of the smart pointers in the list to delete the object it's holding.

    Which smart pointer would do this for me? I want the one that incurs the least amount of overhead.

    I'm using 4.6, so I have access to all of the Qt smart pointers.

    Similar question was posted here, but the guy uses Boost's smart pointer.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt 4.6: Which Qt smart pointer to use for this case?

    Can the list be deleted without you knowing? If not then use regular pointers and simply call qDeleteAll() on the list before you delete the list itself. Smart pointers would make sense here if something else could have deleted objects from your list (i.e. resized down the list without deleting the referenced objects first).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Tags for this Thread

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.