Skip to content

Commit 06df3b2

Browse files
committed
fix: access violation when pressing Tab in a grid cell editor
1 parent 9fd04a4 commit 06df3b2

2 files changed

Lines changed: 52 additions & 31 deletions

File tree

‎source/extra_controls.pas‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface
88
Winapi.ShLwApi, Vcl.ExtCtrls, VirtualTrees, VirtualTrees.Types, SynRegExpr, Vcl.Controls, Winapi.ShlObj,
99
SynEditMiscClasses, SynUnicode, Vcl.Themes, Vcl.GraphUtil, Math;
1010

11+
const
12+
// Posted by TExtForm.DeferFreeControl, see there
13+
WM_HEIDISQL_DEFERREDFREE = WM_USER + $0D00;
14+
1115
type
1216
// Form with a sizegrip in the lower right corner, without the need for a statusbar
1317
TExtForm = class(TForm)
@@ -24,6 +28,8 @@ TExtForm = class(TForm)
2428
public
2529
constructor Create(AOwner: TComponent); override;
2630
class procedure InheritFont(AFont: TFont);
31+
procedure WMDeferredFree(var Message: TMessage); message WM_HEIDISQL_DEFERREDFREE;
32+
procedure DeferFreeControl(AControl: TControl);
2733
property HasSizeGrip: Boolean read GetHasSizeGrip write SetHasSizeGrip default False;
2834
class procedure FixControls(ParentComp: TComponent);
2935
class procedure SaveListSetup(List: TVirtualStringTree);
@@ -148,6 +154,21 @@ procedure TExtForm.DoShow;
148154
inherited;
149155
end;
150156

157+
procedure TExtForm.WMDeferredFree(var Message: TMessage);
158+
begin
159+
TObject(Message.WParam).Free;
160+
end;
161+
162+
procedure TExtForm.DeferFreeControl(AControl: TControl);
163+
begin
164+
// Free AControl after the current message chain has finished. Needed when
165+
// the control's own message handling is on the call stack, e.g. when an
166+
// editor control's OnKeyDown event ends a grid edit which destroys the
167+
// editor link. Freeing the control synchronously would let the VCL message
168+
// dispatch continue on a dead object, see issue #2439.
169+
PostMessage(Handle, WM_HEIDISQL_DEFERREDFREE, WPARAM(AControl), 0);
170+
end;
171+
151172

152173
procedure TExtForm.DoBeforeMonitorDpiChanged(OldDPI, NewDPI: Integer);
153174
begin

‎source/grideditlinks.pas‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ TAllKeysCheckBox = class(TCheckBox)
2222

2323
TBaseGridEditorLink = class(TInterfacedObject, IVTEditLink)
2424
private
25-
FInstanceId: Integer;
2625
FParentForm: TWinControl; // A back reference to the main form
2726
FTree: TVirtualStringTree; // A back reference to the tree calling.
2827
FNode: PVirtualNode; // The node to be edited.
@@ -39,7 +38,6 @@ TBaseGridEditorLink = class(TInterfacedObject, IVTEditLink)
3938
FModified: Boolean;
4039
FAllowEdit: Boolean;
4140
FBeginEditTime: Cardinal;
42-
procedure Log(Msg: String);
4341
procedure TempWindowProc(var Message: TMessage);
4442
procedure DoKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
4543
procedure DoEndEdit(Sender: TObject);
@@ -62,6 +60,11 @@ TBaseGridEditorLink = class(TInterfacedObject, IVTEditLink)
6260
function GetBounds: TRect; virtual; stdcall; // Normally useless and unused
6361
procedure ProcessMessage(var Message: TMessage); stdcall;
6462
procedure SetBounds(R: TRect); virtual; stdcall; abstract;
63+
// Called from destructors to free editor controls without crashing:
64+
// our destructor regularly runs while such a control is still processing
65+
// a message, as its OnKeyDown event ends the edit which destroys us.
66+
// Deferring the destruction until that message is done, see issue #2439.
67+
procedure ReleaseEditorControl(AControl: TWinControl);
6568
end;
6669

6770
THexEditorLink = class(TBaseGridEditorLink)
@@ -237,11 +240,6 @@ procedure TAllKeysCheckBox.WMGetDlgCode(var Msg: TMessage);
237240
Msg.Result := Msg.Result or DLGC_WANTALLKEYS;
238241
end;
239242

240-
procedure TBaseGridEditorLink.Log(Msg: String);
241-
begin
242-
MainForm.LogSQL('#'+FInstanceId.ToString+': '+Msg, lcDebug);
243-
end;
244-
245243

246244
constructor TBaseGridEditorLink.Create;
247245
begin
@@ -252,7 +250,6 @@ constructor TBaseGridEditorLink.Create;
252250
constructor TBaseGridEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boolean; Col: TTableColumn);
253251
begin
254252
inherited Create;
255-
FInstanceId := Random(100);
256253
FTree := Tree;
257254
// Enable mouse scrolling, plus ensure the editor component
258255
// is not partly hidden when it pops up in a bottom cell
@@ -373,6 +370,23 @@ function TBaseGridEditorLink.EndEditHelper(NewText: String): Boolean;
373370
FTree.SetFocus;
374371
end;
375372

373+
procedure TBaseGridEditorLink.ReleaseEditorControl(AControl: TWinControl);
374+
begin
375+
// Do not free editor controls synchronously here. Our destructor regularly
376+
// runs while such a control is still processing a message, as its OnKeyDown
377+
// event ends the edit which destroys us. Freeing the control here would let
378+
// the VCL message dispatch continue on a dead object, causing the
379+
// EAccessViolations reported in issue #2439. Deferring the destruction until
380+
// the current message is done keeps the control alive for the rest of the
381+
// VCL's key dispatch.
382+
if (csDestroying in AControl.ComponentState) or (csCreating in AControl.ControlState) then
383+
Exit;
384+
if FParentForm is TExtForm then
385+
TExtForm(FParentForm).DeferFreeControl(AControl)
386+
else
387+
AControl.Free;
388+
end;
389+
376390
procedure TBaseGridEditorLink.TempWindowProc(var Message: TMessage);
377391
begin
378392
case Message.Msg of
@@ -381,16 +395,8 @@ procedure TBaseGridEditorLink.TempWindowProc(var Message: TMessage);
381395
FOldWindowProc(Message);
382396
WM_GETDLGCODE: // "WantTabs" mode for main control
383397
Message.Result := Message.Result or DLGC_WANTARROWS or DLGC_WANTALLKEYS or DLGC_WANTTAB;
384-
else begin
385-
try
386-
FOldWindowProc(Message);
387-
except
388-
// EAccessViolation occurring in some cases
389-
on E:Exception do begin
390-
Log(E.Message+' Message CharCode:'+TWMChar(Message).CharCode.ToString+' Msg:'+Message.Msg.ToString);
391-
end;
392-
end;
393-
end;
398+
else
399+
FOldWindowProc(Message);
394400
end;
395401
end;
396402

@@ -575,9 +581,7 @@ destructor TDateTimeEditorLink.Destroy;
575581
begin
576582
AppSettings.WriteInt(asDateTimeEditorCursorPos, FMaskEdit.SelStart, IntToStr(Integer(FTableColumn.DataType.Category)));
577583
FreeAndNil(FTimer);
578-
FreeAndNil(FUpDown);
579-
FreeAndNil(FMaskEdit);
580-
FreeAndNil(FPanel);
584+
ReleaseEditorControl(FPanel);
581585
inherited;
582586
end;
583587

@@ -927,7 +931,7 @@ constructor TEnumEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boolean;
927931

928932
destructor TEnumEditorLink.Destroy;
929933
begin
930-
FCombo.Free;
934+
ReleaseEditorControl(FCombo);
931935
inherited;
932936
end;
933937

@@ -1045,7 +1049,7 @@ constructor TSetEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boolean;
10451049

10461050
destructor TSetEditorLink.Destroy;
10471051
begin
1048-
FreeAndNil(FPanel);
1052+
ReleaseEditorControl(FPanel);
10491053
inherited;
10501054
end;
10511055

@@ -1176,11 +1180,7 @@ constructor TInplaceEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boole
11761180

11771181
destructor TInplaceEditorLink.Destroy;
11781182
begin
1179-
if not ((csDestroying in FPanel.ComponentState) or (csCreating in FPanel.ControlState)) then begin
1180-
FEdit.Free;
1181-
FButton.Free;
1182-
FPanel.Free;
1183-
end;
1183+
ReleaseEditorControl(FPanel);
11841184
inherited;
11851185
end;
11861186

@@ -1424,7 +1424,7 @@ constructor TColumnDefaultEditorLink.Create(Tree: TVirtualStringTree; AllowEdit:
14241424

14251425
destructor TColumnDefaultEditorLink.Destroy;
14261426
begin
1427-
FPanel.Free;
1427+
ReleaseEditorControl(FPanel);
14281428
inherited;
14291429
end;
14301430

@@ -1675,8 +1675,8 @@ constructor TDataTypeEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Bool
16751675

16761676
destructor TDataTypeEditorLink.Destroy;
16771677
begin
1678-
FreeAndNil(FTreeSelect);
1679-
FreeAndNil(FMemoHelp);
1678+
ReleaseEditorControl(FTreeSelect);
1679+
ReleaseEditorControl(FMemoHelp);
16801680
inherited;
16811681
end;
16821682

0 commit comments

Comments
 (0)