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.
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.
- Read and copy task inputs on the UI thread.
- Perform file, network or compute work without directly using captured raw DOM pointers.
- Dispatch with Platform::postToMainThread or the binding’s equivalent.
- 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.
| Symptom | Check and action |
|---|---|
| Crash after reload | Look up nodes and attach handlers again instead of reusing addresses from the old document. |
| Callbacks after closing | Cancel work or check validity in callbacks; remove long-lived listeners and timers. |
| Intermittent freezes | Look for synchronous I/O in UI callbacks and ensure the message loop continues. |
Something unclear?
Report an issue on this page →