If QScopedPointer is used:
QScopedPointer<Apple> m_apple;
QScopedPointer<Apple> m_apple;
To copy to clipboard, switch view to plain text mode
There are two scenarios:
1. m_apple was closed by user during runtime, and was deleted automatically since WA_deleteonclose is true. When Class1 is deleted, QScopedPointer deletes a dangling pointer since m_apple is not 0. Double-deletion, so runtime error.
2. m_apple was not closed by user during runtime. When Class1 is deleted, QScopedPointer deletes m_apple automatically. No runtime error since m_apple has not been deleted before.
If QPointer is used:
QPointer<Apple> m_apple;
QPointer<Apple> m_apple;
To copy to clipboard, switch view to plain text mode
There are two scenarios:
1. m_apple was closed by user during runtime, and was deleted automatically since WA_deleteonclose is true. When Class1 is deleted, m_apple is already deleted, so no memory leak.
2. m_apple was not closed by user during runtime. When Class1 is deleted, nobody deletes m_apple, memory leak.
As I said I can just use QPointer and put 'delete m_apple' in Class1::~Class1(), but I want to see if QScopedPointer and QPointer work together.
So if QScopedPointer and QPointer is combined:
QScopedPointer < QPointer<Apple> > m_apple;
QScopedPointer < QPointer<Apple> > m_apple;
To copy to clipboard, switch view to plain text mode
There are two scenarios:
1. m_apple was closed by user during runtime, and was deleted automatically since WA_deleteonclose is true. When Class1 is deleted, QScopedPointer should delete a QPointer which is already 0, which is OK, and no double-deletion.
2. m_apple was not closed by user during runtime. When Class1 is deleted, QScoped deletes m_apple.
Bookmarks