Why do official examples and tutorials about the Qt library never make use of smart pointers? I only see new
and delete
for creating and destroying the widgets.
I searched for the rationale but I could not find it, and I don't see one myself except if it's for historic reasons or backward compatibility: not everyone wants the program to terminate if a widget constructor fails, and handling it via try/catch blocks is just ugly (even if used in few places). The fact the parent widgets may take the ownership of the children also only partially explains the thing to me, as you would still have to use delete
for the parents at some level.
Purpose. A smart pointer is an abstract data type that has all features of a standard pointer and additionally provides automatic garbage collection. Smart pointers facilitate the dynamic memory operations. Their main advantage is reducing memory leaks and bugs due to poor memory management.
Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes out of scope, the pointed object is destroyed too.
Use when you want to assign one raw pointer to multiple owners, for example, when you return a copy of a pointer from a container but want to keep the original. The raw pointer is not deleted until all shared_ptr owners have gone out of scope or have otherwise given up ownership.
Smart pointers are class objects that behave like raw pointers but manage objects that are new and when or whether to delete them— smart pointers automatically delete the managed object at the appropriate time.
A smart pointer is an abstract data type that has all features of a standard pointer and additionally provides automatic garbage collection. Smart pointers facilitate the dynamic memory operations. Their main advantage is reducing memory leaks and bugs due to poor memory management. Smart Pointers in Qt.
A smart pointer is an abstract data type that has all features of a standard pointer and additionally provides automatic garbage collection. Smart pointers facilitate the dynamic memory operations. Their main advantage is reducing memory leaks and bugs due to poor memory management.
The examples are part of the Qt packages. Visit the Downloads page for more information. Open and run examples within Qt Creator's Welcome mode. Most of the examples run on various platforms and to search for platform-specific examples, type the platform name (or any keywords) in the search field.
Open and run examples within Qt Creator's Welcome mode. Most of the examples run on various platforms and to search for platform-specific examples, type the platform name (or any keywords) in the search field. For example, typing Android in the search field lists the examples that are fully compatible with Android.
Because Qt relies on a parent-child model to manage Qobject resources. It follows the composite + Chain-of-responsibility pattern, which is used from event management to memory management, drawing, file handling, etc...
Actually, trying to use a QObject in a shared\unique pointer is overengineering (99% of the time).
deleteLater
directly.That said, you can still use RAII with Qt. For instance QPointer behaves as a weak reference on a QObject
. I would use QPointer<QWidget>
rather than QWidget*
.
note: to not sound too fanboy, two words : Qt + valgrind.
The smart pointer classes std::unique_ptr
and std::shared_ptr
are for memory management. Having such a smart pointer means, that you own the pointer. However, when creating a QObject
or a derived type with a QObject
parent, the ownership (the responsibility to clean up) is handed over to the parent QObject
. In that case, the standard library smart pointers are unnecessary, or even dangerous, since they can potentially cause a double deletion. Yikes!
However, when a QObject
(or derived type) is created on the heap without a parent QObject
things are very different. In that case you should not just hold a raw pointer, but a smart pointer, preferably a std::unique_ptr
to the object. That way you gain resource safety. If you later hand the object ownership to a parent QObject
you can use std::unique_ptr<T>::release()
, like so:
auto obj = std::make_unique<MyObject>(); // ... do some stuff that might throw ... QObject parentObject; obj->setParent( &parentObject ); obj.release();
If the stuff you do before giving your orphan a parent throws an exception, then you would have a memory leak, if you used raw pointer to hold the object. But the code above is save against such a leak.
It is not modern C++ advice to avoid raw pointers all together, but to avoid owning raw pointers. I might add another modern C++ advice: Don't use smart pointers for objects that are owned by some other program entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With