Ivan Akulov
Ivan Akulov

Reputation: 4323

Initialization of a pointer immediately after calling deleteLater()

Is it safe to immediately initialize a Qt4 pointer after calling deleteLater()? I.e. is the next code safe?

QLabel *label = new QLabel("Text");
// doing smth
label->deleteLater();
label = new QLabel("Other text");

Upvotes: 5

Views: 430

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476990

According to the documentation (which you are welcome to consult yourself in the future), your code is perfectly fine. But note that your question is wrong, since you are not "initializing" label a second time; you are merely assigning to it.

After the assignment, label simply points to an entirely different, new object, and the original object is registered for eventual deletion, so all is well.

Upvotes: 5

Related Questions