Skip to content

fix(developer): handle window already destroyed when closing editor#15618

Open
mcdurdin wants to merge 1 commit intomasterfrom
fix/developer/15617-handle-null-window-on-destroy
Open

fix(developer): handle window already destroyed when closing editor#15618
mcdurdin wants to merge 1 commit intomasterfrom
fix/developer/15617-handle-null-window-on-destroy

Conversation

@mcdurdin
Copy link
Member

Fixes: #15617
Fixes: KEYMAN-DEVELOPER-1JD
Fixes: KEYMAN-DEVELOPER-36K
Fixes: KEYMAN-DEVELOPER-2P5
Test-bot: skip

Fixes: #15617
Fixes: KEYMAN-DEVELOPER-1JD
Fixes: KEYMAN-DEVELOPER-36K
Fixes: KEYMAN-DEVELOPER-2P5
Test-bot: skip
@keymanapp-test-bot
Copy link

keymanapp-test-bot bot commented Feb 24, 2026

User Test Results

Test specification and instructions

User tests are not required

Test Artifacts

  • Windows
    • Keyman for Windows - build : all tests passed (no artifacts on BuildLevel "build")
    • FirstVoices Keyboards for Windows - build : all tests passed (no artifacts on BuildLevel "build")
    • FirstVoices Keyboards for Windows (old PRs) - build : all tests passed (no artifacts on BuildLevel "build")
    • Keyman for Windows (old PRs) - build : all tests passed (no artifacts on BuildLevel "build")
    • Text Editor (ARM64) - build : all tests passed (no artifacts on BuildLevel "build")
    • Text Editor (x64) - build : all tests passed (no artifacts on BuildLevel "build")
    • Text Editor (x86) - build : all tests passed (no artifacts on BuildLevel "build")

@keymanapp-test-bot keymanapp-test-bot bot added this to the A19S23 milestone Feb 24, 2026
Copy link
Contributor

@darcywong00 darcywong00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

begin
AssertVclThread;
Result := cefwp.HandleAllocated and IsChild(cefwp.Handle, GetFocus);
Result := Assigned(cefwp) and cefwp.HandleAllocated and IsChild(cefwp.Handle, GetFocus);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the Assigned(cefwp) check in SetFocus as well?

At least that's what devin.ai thinks.

common/windows/delphi/chromium/Keyman.UI.UframeCEFHost.pas:R376

Missing Assigned(cefwp) check in SetFocus causes access violation when cefwp is nil

The PR adds Assigned(cefwp) guards in HasFocus and Handle_CEF_DESTROY to protect against cefwp being nil, but the same guard is missing in SetFocus at line 376. After Handle_CEF_DESTROY runs FreeAndNil(cefwp) at line 579, cefwp becomes nil. If SetFocus is subsequently called (e.g., from Handle_CEF_LOADEND at line 643 via an asynchronously posted CEF_LOADEND message), it will access cefwp.CanFocus on a nil reference, causing an access violation.

Root Cause

The destruction sequence is asynchronous: cefClose posts CEF_DESTROY (line 570), and Handle_CEF_DESTROY frees cefwp via FreeAndNil(cefwp) (line 579). However, other messages like CEF_LOADEND can still be in the message queue. When Handle_CEF_LOADEND (Keyman.UI.UframeCEFHost.pas:634-647) processes, it calls SetFocus at line 643, which accesses cefwp.CanFocus without checking if cefwp is assigned:

if not FIsClosing and cefwp.CanFocus and Assigned(cef) then

The fix in Handle_CEF_SETFOCUS (line 665) already has the correct pattern: if Assigned(cefwp) and cefwp.Visible and cefwp.CanFocus then. The same pattern should be applied in SetFocus.

Impact: Access violation crash when the browser is being destroyed and a load-end or external SetFocus call arrives.

@keyman-server keyman-server modified the milestones: A19S23, A19S24 Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

bug(developer): EAccessViolation: Exception EAccessViolation in module tike.exe at 0058E628.

4 participants