PDA

View Full Version : Qt 4.6: Which Qt smart pointer to use for this case?



ShaChris23
12th November 2009, 01:53
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 (http://stackoverflow.com/questions/789532/qlist-and-sharedptr), but the guy uses Boost's smart pointer.

wysota
12th November 2009, 03:11
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).