3

When I delete the object the QPointer is pointing to, I check the value of the QPointer, and it is not NULL, but when I check its isNull function, it returns true.

And more strangely, when I do (!m_qpointer) it also returns true. So how is this possible?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Nyaruko
  • 4,329
  • 9
  • 54
  • 105
  • "When I delete the object the QPointer is pointing to ", yes, you are deleting the object, rather than the pointer. – Zen Oct 21 '15 at 00:15
  • --->Object , now delete Object: ---->MemoryAddressOfObject, As you can see the pointer is still there and pointing to the same address, but the object is not valid anymore. – Sebastian Lange Oct 21 '15 at 05:12

1 Answers1

4

(!m_qpointer) returns true when you delete the object it is pointing to, because of this operator defined in qpointer.h:

inline operator T*() const
    { return static_cast<T*>(const_cast<QObject*>(o)); }

It returns the pointer it is guarding. If it has been deleted, then it will be null.

isNull() returns true if the pointer it is guarding is null:

inline bool isNull() const
    { return !o; }

Now I'm not sure what do you mean by I check the value of the QPointer, and it is not NULL. Why should it be null? The QPointer object should still be a valid object even after deleting the pointer it is guarding.

thuga
  • 12,601
  • 42
  • 52