PDA

View Full Version : How to mix QScopedPointer with QPointer



jezz
11th April 2012, 15:20
I have a class like this:


class Class1
{
public:

Apple * apple();

private:
QScopedPointer< QPointer<Apple> > m_apple;
};


Apple is a QDialog.
How do I instantiate m_apple in apple()?
I tried the following but could not compile:


Apple * Class1::apple() {
if (m_apple == 0) {
m_apple->reset(new Apple());
}
return m_apple;
}

high_flyer
12th April 2012, 21:23
Can you explain what you actually think this statement does?:

QScopedPointer< QPointer<Apple> > m_apple;

jezz
13th April 2012, 18:21
Well, first I want Class1 to automatically delete m_apple on destruction, hence QScopedPointer.
Then I also want m_apple to be automatically set to 0 when it is deleted, hence QPointer.
Qt::WA_DeleteOnClose is set to true in Apple.

Of course I can just "delete m_apple" in destructor, but I want to see if this works.

amleto
13th April 2012, 19:45
right.... so please say what is wrong with


QScopedPointer<Apple> m_apple;


And what do you think will happen if Apple has been deleted (because WA_deleteonclose is true), and the the destructor is called? Hint - double deletion.

jezz
13th April 2012, 23:59
If QScopedPointer is used:

QScopedPointer<Apple> m_apple;
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;
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;
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.

wysota
14th April 2012, 09:20
I completely I completely fail to see the point of deleting the Apple instance when it is closed if next time you call apple(), it will be recreated again. Especially that if the dialog contains any valuable information, you won't be able to access it once the dialog is closed (and thus destructed).I completely fail to see the point of deleting the Apple instance when it is closed if next time you call apple(), it will be recreated again. Especially that if the dialog contains any valuable information, you won't be able to access it once the dialog is closed (and thus destructed).

The simplest thing you can do is this:


class Class1 {
const Apple * apple() const { return &m_apple; }
private:
Apple m_apple;
};

or just set a parent for the dialog that will delete it when it itself is getting deleted.