FORGE KUI / docs

Windows, threads and lifecycle

Initialize the platform, create a window, load its document and run the event loop.

Version 3.61.7

Window startup and document replacement

Initialize the platform, create a window, load its document and run the event loop. Release host resources after closing the window, and do not keep old element pointers after a document reload.

UI thread and callback captures

UI work runs on the main thread. Return background network or file results through Platform::postToMainThread or the binding's task-posting method. Objects captured by a callback must still be alive when it runs.

Ownership in the lesson project

Window owns Document, which owns its elements. SettingsState lives in the outer event-loop stack frame until run returns. This project does not reload its document; reloading requires removing old callbacks and looking up new elements.

cpp
    reset->addEventListener("click", [&](kui::Event&) {
        name->setValue(state.name);
        notifications->setChecked(state.notifications);
        name->removeAttribute("aria-invalid");
        status->setAttribute("data-state", "idle");
        status->setTextContent(english ? "Restored the last saved values." : "已恢复上次保存的内容。");
    });

Return async work to the UI

Run work in the background, then return DOM updates to the UI thread.

  1. Read and copy task inputs on the UI thread.
  2. Perform file, network or compute work without directly using captured raw DOM pointers.
  3. Dispatch with Platform::postToMainThread or the binding’s equivalent.
  4. Check that the window and page remain valid; discard results for a closed or replaced page.

Lifetime failures

Use the symptom to narrow the cause before editing.

Lifetime failures
SymptomCheck and action
Crash after reloadLook up nodes and attach handlers again instead of reusing addresses from the old document.
Callbacks after closingCancel work or check validity in callbacks; remove long-lived listeners and timers.
Intermittent freezesLook for synchronous I/O in UI callbacks and ensure the message loop continues.