In my project I create QObject
instances and give them a parent-child relationship. I also want to keep track of some of the objects with QSharedPointer
instances.
When an object gets deleted, either by delete childObject;
or delete parentObject;
, I would like the QSharedPointer
instances to return true
when calling isNull()
.
However, upon deleting any object QSharedPointer
points to, my program crashes.
Here is my code:
QObject *child = new QObject();
QSharedPointer<QObject> ptr(child);
delete child;
This alone crashes my program. I read the documentation for QSharedPointer
and it states A QSharedPointer object can be created from a normal pointer
so I'm a bit confused as to why QSharedPointer
can't handle the object pointed to being deleted.
QPointer
doesn't have this problem, and I can even call isNull()
, however the reason I need QSharedPointer
is because some child objects will be created and not given a parent, at which point I want the child object to be deleted when all relevant QSharedPointers
go out of scope.