본문 바로가기

Qt

Qt deleteLater

Qt Source에는 이렇게 되어었다. 

/*!
    Schedules this object for deletion.

    The object will be deleted when control returns to the event
    loop. If the event loop is not running when this function is
    called (e.g. deleteLater() is called on an object before
    QCoreApplication::exec()), the object will be deleted once the
    event loop is started.

    Note that entering and leaving a new event loop (e.g., by opening a modal
    dialog) will \e not perform the deferred deletion; for the object to be
    deleted, the control must return to the event loop from which
    deleteLater() was called.

    \bold{Note:} It is safe to call this function more than once; when the
    first deferred deletion event is delivered, any pending events for the
    object are removed from the event queue.

    \sa destroyed(), QPointer
*/
void QObject::deleteLater()
{
    QCoreApplication::postEvent(this, new QEvent(QEvent::DeferredDelete));
}

 

DeferredDelete 이라는 이벤트를 하나 보낸다. 주석처럼 이벤트 스케쥴러가 동작되면 해당 이벤트를 이벤트리스트에서 꺼내 처리하고 결국 object 삭제를 처리한다.

Scheduler를 통해 최종적으로는 QObject의 event 처리 함수를 통해 객체가 삭제된다.
bool QObject::event(QEvent *e)
    ...
    case QEvent::DeferredDelete:        
        delete this;
        break;
    ... 
}