How deleteLater() actually works in qt?

c++, qt

Solution

`QObject::deleteLater()` is just a plain method of QObject. You cannot call a method on a pointer, only on the object the pointer points to. So deleteLater() operates on the object the pointer variable `o` is pointing to (`*o`) at the time you call deleteLater(). It cannot know about the local pointer variable you're using (`o`), which is your private business.

To share the actual pointer variable, it would be necessary to pass it somewhere via a QObject** (pointer to QObject pointer;`&o`), which you don't.

In QObject::deleteLater(), the method refers to the object it's called on via the `this` pointer, independent of any other variables that refer to the same object (such as `o`).

Problem

Barebones example: ``` Object * o = new Object(); //let's call this one object A on heap o->deleteLater(); o = new Object(); //and let's call this one object B on heap ``` Is deleteLater() going to delete A or B when we enter the event loop? I guess my question is, does deleteLater() make a copy of the pointer at the point of calling or does it delete whatever pointer holds when entering the event loop? If the latter is true, how can we deleteLater() a pointer which we need to reinitialize?

Original source