QSharedPointer and QObject::deleteLater
Asked Answered
M

2

9

I have a situation where a QSharedPointer managed object signalizes that it has finished it's purpose and is ready for deletion soon (after execution left the function emitting my readyForDeletion signal). When working with normal pointers, I'd just call QObject::deleteLater on the object, however this isn't possible with a QSharedPointer-managed instance. My workaround is the following:

template<typename T>
class QSharedPointerContainer : public QObject
{
   QSharedPointer<T> m_pSharedObj;

public:

   QSharedPointerContainer(QSharedPointer<T> pSharedObj)
      : m_pSharedObj(pSharedObj)
   {} // ==> ctor

}; // ==> QSharedPointerContainer

template<typename T>
void deleteSharedPointerLater(QSharedPointer<T> pSharedObj)
{
   (new QSharedPointerContainer<T>(pSharedObj))->deleteLater();
} // ==> deleteSharedPointerLater

This works well, however there's a lot of overhead using this method (allocating a new QObject and so on). Is there any better solution to handle such situations?

Menchaca answered 27/9, 2012 at 14:28 Comment(3)
So you have an object that is managed by shared ownership (there are several objects that claim ownership) yet at the same time it manages its own lifetime (by having a readyForDeletion signal)? Sounds to me like you've got a really messed up ownership situation. Instead of looking for workarounds, try to clear up who is really responsible for the object and therefore who should delete it.Glorify
I have to agree with @Sabastian. I was about to post a similar comment when his came in. If your object keeps track of when it is safe to be deleted, just use a plain old pointer, and handle the readyForDeletion signal on whatever class creates the object in the first place.Cushing
Thank you for your comments. In my actual program things are a lot more complicated, however, taking a look at the abstract problem, it becomes clear that using a QSharedPointer in this context is no good idea at all.Menchaca
R
22

You can use the QSharedPointer constructor with the Deleter :

The deleter parameter specifies the custom deleter for this object. The custom deleter is called, instead of the operator delete(), when the strong reference count drops to 0. This is useful, for instance, for calling deleteLater() on a QObject instead:

 QSharedPointer<MyObject> obj =
         QSharedPointer<MyObject>(new MyObject, &QObject::deleteLater);
Rubella answered 27/9, 2012 at 14:44 Comment(0)
V
1

An alternative is using QPointer instead of QSharedPointer, citing the documentation:

The QPointer class is a template class that provides guarded pointers to QObject.

A guarded pointer, QPointer, behaves like a normal C++ pointer T *, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which become "dangling pointers" in such cases). T must be a subclass of QObject.

Venter answered 16/3, 2018 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.