Adding to RobbiE's answer, the QPointer and QSharedPointer are two complemetary classes that serve different functions.
QPointer and its Caveats
A QPointer
is a weak pointer to a QObject
. It resets itself to zero when the pointed-to object is destroyed. It is not an owning pointer: it never deletes the object itself, and it doesn't guarantee the object's existence. Use it to avoid having a dangling pointer to an object whose ownership is managed elsewhere. Check whether the pointer is null before each use. You will run into race conditions if the object is destructed in another thread:
if (pointer) /* another thread can destruct it here */ pointer->method();
The QPointer
itself is thread-safe, but the code that uses it cannot ever be thread-safe due to the insufficient API provided by QPointer
.
The QPointer
is always safe to use from the main thread with widget objects, and with the objects owned by widget objects where the parent-child relationship is established. The objects and their users are in the same thread, so the object will not be disposed by another thread between the pointer null check and the use of the pointer:
QPointer<QLabel> label(...);
if (label) label->setText("I'm alive!");
You need to be careful if you're reentering the event loop. Suppose we have:
QPointer<QLabel> label(...);
...
if (label) {
label->setText(...)
QFileDialog::getOpenFileName(...);
// Here the event loop is reentered, and essentially any other code in your
// application can run, including code that could destruct the widget that
// you're using. The `deleteLater` calls won't do it, since they defer to
// the main event loop, but it's not always obvious that nothing else
// will. The line below can thus dereference a null pointer (IOW: crash).
label->setText(...);
}
At the very least, you need to re-check the QPointer
every time after you invoke principally unrelated code - e.g. emit a signal (anyone can do anything in reaction to it!), return an event-loop-reentering call like exec
. etc. That's also why blocking calls are evil: you should never use them.
QPointer<QWidget> widget(...);
...
if (label) {
label->setText(...);
QFileDialog::getOpenFileName(...);
// Reenters the event loop, the widget may get deleted.
}
// Not re-checking the pointer here would be a bug.
if (label) {
label->setText(...);
...
}
QSharedPointer and QWeakPointer
This section is left as a reference. In modern code, you should be using std::shared_ptr
and std::weak_ptr
, without any reservations. They have been in C++ for 7 years as of 2018.
A QSharedPointer
is an owning pointer. It works like variables in Java and CPython, or like std::shared_ptr
. As long as there is at least one QSharedPointer
pointing to an object, the object is kept around. When the last QSharedPointer
is destructed, the object gets destructed and deleted.
The QWeakPointer
is QSharedPointer
's cousin. It is non-owning. It tracks whether the objects held by QSharedPointer
s are still alive. It resets itself to nullptr
when the last QSharedPointer
that owns the object goes away. It can be thought of as a generalization of QPointer
to non-QObject
classes. The only safe way to use a QWeakPointer
is to convert it to a QSharedPointer
. When you hold a shared pointer, the object will be guaranteed to stay alive.
A QPointer
is like a QWeakPointer
for QObject
s, but it doesn't require the existence of a QSharedPointer
.
It is an error to use a QSharedPointer
on an object that's not allocated on the heap, and on an object whose lifetime is managed by other mechanisms. For example, it's an error to have a QSharedPointer
to a QObject
that has a parent. The object's parent would delete it, and you would end up with a dangling QSharedPointer
! Qt has some built-in checks that issue warnings when that happens, but by that time it's too late and undefined behavior has struck.
QScopedPointer
This section is left as a reference. You should be using std::unique_ptr
, without any reservations. It has been in C++ for 7 years as of 2018.
QScopedPointer
, just like std::unique_ptr
, is a solely owning pointer. Its job is to delete the held object when it goes out of scope. The C++11 unique_ptr
's name is very apt: it is a unique pointer, in the sense that it's an error to try and copy such pointers. There is always only one QScopedPointer
that owns a given object, and it does not cooperate with other smart pointer types. You can fetch a raw pointer to underlying object by calling the data
method.
std::auto_ptr
This pointer was an attempt at working around lack of move semantics in C++98/03. Due to its broken copy semantics, the use of this class should be treated as a bug. Use std::unique_ptr
or std::shared_ptr
- the former if it suffices that it be movable, the latter if several copies of it must coexist.