Our application is VERY poorly designed - it uses both std::shared_ptr
and QObject
parent-child relationship to manage some of our objects. This leads to segfaults when QObject::~QObject
deletes its child objects and THEN shared_ptr
tries to delete it also.
We discussed this but currently see no way to fix this easily so I'll need to use few dirty hacks until we fix it.
Now what I have is a std::shared_ptr<MyObject> getMyObjectPtr()
function. I need to put the result into QPointer<MyObject>
- QPointers are weak pointers that merely indicate if the QObject they manage is deleted. I cannot change that function in any way, it would break the whole application.
I tried few hacks with custom dealocator but that doesn't seem to work.
// get the shared_ptr from inacessible API
std::shared_ptr<MyObject> oldPtr(getMyObjectPtr());
// create a new pointer that never deletes it's value
std::shared_ptr<MyObject> newPtr(nullptr, [](MyObject*) {});
// Move pointer from old to new non-deleting ptr
newPtr.swap(oldPtr);
QPointer<AutoUpdateTable> qptr(newPtr.get());
However this way, the pointer gets deleted once my function ends. Presumably, the custom dealocator moves along with the data.
Aucun commentaire:
Enregistrer un commentaire