mirror of
https://github.com/lubosz/overte.git
synced 2025-04-24 21:23:18 +02:00
Cleaner destruction handling
This commit is contained in:
parent
b2dac6f53f
commit
3f2b5aeca4
1 changed files with 9 additions and 12 deletions
|
@ -20,7 +20,14 @@
|
|||
public:\
|
||||
typedef QSharedPointer<T> SharedPointer;\
|
||||
private:\
|
||||
void customDeleter() { delete this; }\
|
||||
void customDeleter() {\
|
||||
QObject* thisObject = dynamic_cast<QObject*>(this);\
|
||||
if (thisObject) {\
|
||||
thisObject->deleteLater();\
|
||||
} else {\
|
||||
delete this;\
|
||||
}\
|
||||
}\
|
||||
friend class DependencyManager;
|
||||
|
||||
class QObject;
|
||||
|
@ -42,17 +49,7 @@ private:
|
|||
|
||||
template <typename T>
|
||||
QSharedPointer<T> DependencyManager::get() {
|
||||
static QSharedPointer<T> sharedPointer;
|
||||
static T* instance = new T();
|
||||
|
||||
if (instance) {
|
||||
if (dynamic_cast<QObject*>(instance) != NULL) { // If this is a QOject, call deleteLater for destruction
|
||||
sharedPointer = QSharedPointer<T>(instance, &T::deleteLater);
|
||||
} else { // Otherwise use custom deleter to avoid issues between private destructor and QSharedPointer
|
||||
sharedPointer = QSharedPointer<T>(instance, &T::customDeleter);
|
||||
}
|
||||
instance = NULL;
|
||||
}
|
||||
static QSharedPointer<T> sharedPointer = QSharedPointer<T>(new T(), &T::customDeleter);
|
||||
return sharedPointer;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue