Skip to content

Commit 1da8683

Browse files
twinn1013ansgarbecker
authored andcommitted
fix: crash when cancelling session manager while a session rename is active
Two independent crashes on macOS were hidden behind this repro, since cancelling the session manager at startup also exits the application: 1. The session manager starts an inline rename right after creating a new session. When the form was closed via Cancel, the edit link's queued async EndEditNode call fired after the tree was already destroyed, and its focus restoration ran into "[TCustomForm.SetFocus] connform:Tconnform Can not focus". Remove pending async edit calls when an editor link is destroyed, cancel a pending inline rename before the form closes, and let TrySetFocus test CanSetFocus, which - unlike CanFocus - also checks the parent form itself, so it does not raise in the first place. 2. On shutdown, TSynBaseCompletionForm.Destroy (SynEdit) first frees its SizeDrag panel and then destroys the window handle. The Cocoa widgetset queries Focused during DestroyHandle, and SynEdit's Focused override dereferences the freed SizeDrag, raising "EObjectCheck: Object reference is Nil" on every macOS shutdown. Work around it by destroying the completion form's handle in FormDestroy while the form is still intact. Fixes issue 2433
1 parent bf46821 commit 1da8683

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

source/apphelpers.pas

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3528,7 +3528,11 @@ procedure TClipboardHelper.SetTryAsText(AValue: String);
35283528
procedure TWinControlHelper.TrySetFocus;
35293529
begin
35303530
try
3531-
if Enabled and CanFocus then
3531+
// CanSetFocus - unlike CanFocus - also tests the parent form itself, so
3532+
// this avoids the "[TCustomForm.SetFocus] ... Can not focus" exception
3533+
// which SetFocus raises on a hidden or disabled form, e.g. while a modal
3534+
// form is closing. See issue 2433.
3535+
if CanSetFocus then
35323536
SetFocus
35333537
else
35343538
MainForm.LogSQL(Self.Name + ': either disabled or cannot focus', lcDebug);

source/connections.pas

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ procedure Tconnform.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
384384
begin
385385
// Modifications? Ask if they should be saved.
386386
FinalizeModifications(CanClose);
387+
// End a pending inline rename while the form is still visible and active.
388+
// Otherwise the edit link tries to restore the focus during form teardown,
389+
// which raises "[TCustomForm.SetFocus] ... Can not focus" on macOS.
390+
// See issue #2433.
391+
if CanClose and ListSessions.IsEditing then
392+
ListSessions.CancelEditNode;
387393
end;
388394

389395

source/grideditlinks.pas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ destructor TBaseGridEditorLink.Destroy;
279279
NewNode: PVirtualNode;
280280
DoPrev: Boolean;
281281
begin
282+
// Drop end/cancel calls which are still queued via DoEndEdit/DoCancelEdit.
283+
// They would fire after this link - and possibly the tree or its form - is
284+
// already destroyed, e.g. when the session manager is cancelled while a
285+
// session rename is active. See issue #2433.
286+
Application.RemoveAsyncCalls(Self);
282287
ActiveGridEditor := nil;
283288
if Assigned(FMainControl) then begin
284289
FMainControl.WindowProc := FOldWindowProc;

source/main.pas

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,11 +1776,29 @@ procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
17761776
CanClose := not (ActiveObjectEditor.DeInit in [mrAbort, mrCancel]);
17771777
end;
17781778

1779+
{$IFDEF DARWIN}
1780+
type
1781+
// Grants access to the protected DestroyHandle, see workaround in FormDestroy
1782+
TWinControlAccess = class(TWinControl);
1783+
{$ENDIF}
1784+
17791785
procedure TMainForm.FormDestroy(Sender: TObject);
17801786
begin
17811787
// Discard a possibly queued AsyncRepaintGrid call
17821788
Application.RemoveAsyncCalls(Self);
17831789

1790+
{$IFDEF DARWIN}
1791+
// Work around a SynEdit bug which crashes the app on every shutdown on Cocoa:
1792+
// TSynBaseCompletionForm.Destroy first frees its SizeDrag panel and then
1793+
// destroys the window handle. The Cocoa widgetset queries Focused while
1794+
// destroying the handle, and the Focused override dereferences the already
1795+
// freed SizeDrag ("EObjectCheck: Object reference is Nil"). Destroying the
1796+
// handle here, while the form is still intact, makes the destructor skip
1797+
// that path. See issue 2433.
1798+
if SynCompletionProposal.TheForm.HandleAllocated then
1799+
TWinControlAccess(SynCompletionProposal.TheForm).DestroyHandle;
1800+
{$ENDIF}
1801+
17841802
// Destroy dialogs
17851803
FreeAndNil(FSearchReplaceDialog);
17861804

0 commit comments

Comments
 (0)