Any difference between std::shared_ptr and QSharedPointer?
-
Is there a difference between std::shared_ptr (now in C++ 11) and QSharedPointer?
I was wondering about this because I was hoping that maybe QSharedPointer understands QObjects, which std::shared_ptr obviously wouldn't. I'm specifically interested in the scenario where I have an instance of QSharedPointer<SomeClass> where SomeClass is derived from QObject and has a QObject parent. If the parent is deleted, then its child should be deleted. What happens to the QSharedPointer instance in that case? I believe std::shared_ptr would be invalid at that point.
Thanks
bmanc
-
See here — http://stackoverflow.com/a/5026705/704503
Qt's containers and other classes similar to STL's ones are:
- aware of Qt architecture (perfomance gains and other “bonuses”);
- designed to replace STL on platforms where it's realization not available;
- a gurantee that there are no diferrences in plenty of realizations.
I personally believe, that you should use Qt's ones in Qt Project. Not a big deal though, if you will decide to switch to STL until your code is consistant (or you know exactly what are you doing and why).
-
[quote author="bmanc" date="1344980719"]I'm still wondering though how does QSharedPointer interact with a QObject?[/quote]
What do you mean? If you think “understanding of QObject” is removing all of it's children, you're are wrong, because this is part of QObject itself (here's quite alright video on topic — "http://qt-project.org/videos/watch/fundamentals-of-qt-objects-in-qt-part-1-3":http://qt-project.org/videos/watch/fundamentals-of-qt-objects-in-qt-part-1-3). -
Sorry, you misunderstood the statement. What I mean is I don't want QSharedPointer to remove a QObject since it's QObject parent will - provided it has a parent. If QSharedPointer understood the QObject hierarchy, then I was hoping it would be smart enough to figure out whether the pointed-to QObject has a parent or not, and handle destruction appropriately.
-
QSharedPointer will do nothing else except delete (no matter what's encapsulated), unless you explicitly specify something other (Deleter).
During delete QObject, it will be disconnected from parent. You can, however, write custom deleter as described in "documentation":http://qt-project.org/doc/qt-4.8/qsharedpointer.html#QSharedPointer-3
-
I've faced somehow similar problem and the answer seems to be "QObjectCleanupHandler":http://qt-project.org/doc/qt-4.8/qobjectcleanuphandler.html
-
[quote author="bmanc" date="1344965308"]Is there a difference between std::shared_ptr (now in C++ 11) and QSharedPointer?
I was wondering about this because I was hoping that maybe QSharedPointer understands QObjects, which std::shared_ptr obviously wouldn't. I'm specifically interested in the scenario where I have an instance of QSharedPointer<SomeClass> where SomeClass is derived from QObject and has a QObject parent. If the parent is deleted, then its child should be deleted. What happens to the QSharedPointer instance in that case? I believe std::shared_ptr would be invalid at that point.
[/quote]Storing a QObject with a parent inside a shared pointer is violating the contract you have with the shared pointer class: it's the only one managing the lifetime of the pointee. In other words, it's perfecty equivalent of doing
@
Object *o = new Object;
SharedPointer<Object> sp(o);
delete o;
@This will incur in a double deletion when the shared pointer's refcount drops to zero, and you'll dereference a dangling pointer if you access the pointer stored in the shared pointer after that the delete has happened.
But IMHO the point is: if you already have a QObject with a parent, you're already managing its lifetime. With a shared pointer you're managing it twice, which will cause all sort of troubles. Are you really sure about your design?
-
I have just found out that the QVector copy constructor (& move constructor) is very different from that of a std::vector! The QVector implements a variation on the COW pattern, whilest std::vector is a deep copy of the object. Here be dragons!
Compare the Qt description http://doc.qt.io/qt-5/qvector.html#QVector-3 versus http://www.cplusplus.com/reference/vector/vector/vector/ -
Hi,
All Qt containers implement COW (Copy On Write).