The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.
More...
template<typename T, typename Cleanup = QScopedPointerDeleter<T>>
class QScopedPointer< T, Cleanup >
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.
Managing heap allocated objects manually is hard and error prone, with the common result that code leaks memory and is hard to maintain. QScopedPointer is a small utility class that heavily simplifies this by assigning stack-based memory ownership to heap allocations, more generally called resource acquisition is initialization(RAII). QScopedPointer guarantees the object pointed to will be deleted when the current scope disappears.
Consider this function which does heap allocations, and have various exit points:
void myFunction(bool useSubClass)
{
MyClass *p = useSubClass ? new MyClass() : new MySubClass;
if (m_value > 3) {
delete p;
delete device;
return;
}
try {
process(device);
}
catch (...) {
delete p;
delete device;
throw;
}
delete p;
delete device;
}
With QScopedPointer the code can be simplified to the following.
void myFunction(bool useSubClass)
{
if (m_value > 3)
return;
process(device);
}
The code the compiler generates for QScopedPointer is the same as when writing it manually. Code that makes use of delete are candidates for QScopedPointer usage (and if not, possibly another type of smart pointer such as QSharedPointer). QScopedPointer intentionally has no copy constructor or assignment operator, such that ownership and lifetime is clearly communicated.
The const qualification on a regular C++ pointer can also be expressed with a QScopedPointer:
Custom cleanup handlers
Arrays as well as pointers that have been allocated with malloc
must not be deleted using delete
. QScopedPointer's second template parameter can be used for custom cleanup handlers.
The following custom cleanup handlers exist:
-
QScopedPointerDeleter - the default, deletes the pointer using
delete
-
QScopedPointerArrayDeleter - deletes the pointer using
delete []
. Use this handler for pointers that were allocated with new []
.
-
QScopedPointerPodDeleter - deletes the pointer using
free()
. Use this handler for pointers that were allocated with malloc()
.
You can pass your own classes as handlers, provided that they have a public static function void cleanup(T *pointer)
.
struct ScopedPointerCustomDeleter
{
static inline void cleanup(MyCustomClass *pointer)
{
myCustomDeallocator(pointer);
}
};
Forward Declared Pointers
Classes which are forward declared can be used within QScopedPointer. However, the destructor of the forward declared class must be available when a QScopedPointer is constructor. This means all classes containing a QScopedPointer which point to a forward declared class must have non-inline constructors, destructors, and assignment operators.
class MyPrivateClass;
class MyClass
{
private:
public:
MyClass();
inline ~MyClass() {}
private:
Q_DISABLE_COPY(MyClass)
};
- See also
- QSharedPointer