The point of shared pointers (and other similar wrappers for pointers) is intended to properly control the destruction of a pointer to an object. This is instead of manually verifying that you are deleting the last copy (and only the last copy), the generic pointer will take care of this for you when it goes beyond. The joint part means that you can create copies of this wrapper object (a shared pointer object), and it will βsplitβ the pointer to the object between the copies (as if you made a copy of a regular pointer) with the added benefit of the above.
As for your code, SomeClass::doStuff() should have a QScopedPointer<QFile> parameter (instead of QIODevice* one), since you are passing it an item that has this type.
Same thing with the MyObject constructor: does it have a parameter of type QPointer<QIODevice> or QSharedPointer<QIODevice> . In general, wherever you used pointers, use QSharedPointer instead (with the appropriate template type). This will save you from the headaches associated with subsequent access to remote objects.
Of course, sometimes you really need a raw QIODevice pointer (for example, to call a third-party library), then you must use the member function data() of the shared pointer object. Just make sure that you do not save (i.e. storage or otherwise copy beyond what is needed) the returned raw pointer, because it undermines the purpose of shared pointers - shared pointers will not know about your extra raw pointer, which is not under control common pointer objects.
EDIT: take() releases ownership of an object with a pointer to a pointer with scope, so when a pointer with scope is destroyed, it does not delete the object. You could use ant in a situation where you transferred ownership of something else - as in your case to MyObject .
source share