I got a problem I'm not sure how to solve..
We have generic objects pool. When object is requested the pool returns QSharedPointer
to the first available instance, with custom Deleter specified. The deleter just returns object to the pool when QSharedPointer
instance ref count is 0.
Everything works just fine for plain objects. It also works fine for QObject
successors, when compiled in Qt 5.
However, if compiled in Qt 4.6 - the problems begin: when same object is requested second time - application exits with an error:
"QSharedPointer: pointer xxx already has reference counting"
I wrote simple test:
QObject* obj = new QObject();
QSharedPointer<QObject> p(obj, deleter); // deleter here does nothing
p.clear();
QSharedPointer<QObject> p2(obj, deleter); // this crashes the app
And surely this fails when compiled in Qt 4.6. Again: works fine in QT 5.x.
Looking into the Qt source code it revealed 4.6 initializes internal ref counter in QObject
when this QObject
is used as a QSharedPointer
parameter. This is done to make sure no two smart pointers can point to the same object and it only gets reset in the destructor.
Qt5 does not check ref counter value when QObject
instance is wrapped into smart pointer, thus it works.
Does anyone know any workaround for older Qt version? Is the any way to completely reset internal Qt status, including ref counter? Any hints are very welcome.
you are only once allowed to create
QSharedPointer
fromQObject
latter you will need to copy that existingQSharedPointer
instanceaccording to Qt 4 and 5 docs:
so your samples behavior is like below:
using QWeakPointer will not delete the
QObject
and the assertion:is to ensure you do not create multiple deleters by accident (which did safe my day, I was using
QSharedPointer
but meantQWeakPointer
) but it does get sometimes in the way.