14

Normally when I delete an object I set it to zero. This helps in that I code my routines to check the object is not zero then proceed.

However if I use the deleteLater() function I have lost control of it. I don't know if its a valid item or if I need to create a new one. The solution I found was just to keep track of the state separately however is there a better way?

NG_
  • 6,895
  • 7
  • 45
  • 67
Phil Hannent
  • 12,047
  • 17
  • 71
  • 118

3 Answers3

23

There is nothing wrong if you call deleteLater() on your object and then set your pointer to nullptr. Qt framework will safely delete the object for you.

As an alternative you can also connect to destroyed signal to be notified of your object's destruction and set your pointer to nullptr in that slot.

NG_
  • 6,895
  • 7
  • 45
  • 67
O.C.
  • 6,711
  • 1
  • 25
  • 26
10

You can use QPointer if the object that is being deleted is a QObject. This will set it to NULL.

sep
  • 3,409
  • 4
  • 29
  • 32
4

Do you mean, "zero the pointer"? In C++ it's more important than in Java to make the distinction between the object and a pointer to it. Here, it's especially important, because if you zero the object its destructor won't work. If you zero a pointer to the object, no problem, because Qt will have added another pointer to the "objects-to-be-deleted" list.

MSalters
  • 173,980
  • 10
  • 155
  • 350