@@ -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;
238241end ;
239242
240- procedure TBaseGridEditorLink.Log (Msg: String);
241- begin
242- MainForm.LogSQL(' #' +FInstanceId.ToString+' : ' +Msg, lcDebug);
243- end ;
244-
245243
246244constructor TBaseGridEditorLink.Create;
247245begin
@@ -252,7 +250,6 @@ constructor TBaseGridEditorLink.Create;
252250constructor TBaseGridEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boolean; Col: TTableColumn);
253251begin
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;
374371end ;
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+
376390procedure TBaseGridEditorLink.TempWindowProc (var Message: TMessage);
377391begin
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 ;
395401end ;
396402
@@ -575,9 +581,7 @@ destructor TDateTimeEditorLink.Destroy;
575581begin
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 ;
582586end ;
583587
@@ -927,7 +931,7 @@ constructor TEnumEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boolean;
927931
928932destructor TEnumEditorLink.Destroy;
929933begin
930- FCombo.Free ;
934+ ReleaseEditorControl( FCombo) ;
931935 inherited ;
932936end ;
933937
@@ -1045,7 +1049,7 @@ constructor TSetEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boolean;
10451049
10461050destructor TSetEditorLink.Destroy;
10471051begin
1048- FreeAndNil (FPanel);
1052+ ReleaseEditorControl (FPanel);
10491053 inherited ;
10501054end ;
10511055
@@ -1176,11 +1180,7 @@ constructor TInplaceEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Boole
11761180
11771181destructor TInplaceEditorLink.Destroy;
11781182begin
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 ;
11851185end ;
11861186
@@ -1424,7 +1424,7 @@ constructor TColumnDefaultEditorLink.Create(Tree: TVirtualStringTree; AllowEdit:
14241424
14251425destructor TColumnDefaultEditorLink.Destroy;
14261426begin
1427- FPanel.Free ;
1427+ ReleaseEditorControl( FPanel) ;
14281428 inherited ;
14291429end ;
14301430
@@ -1675,8 +1675,8 @@ constructor TDataTypeEditorLink.Create(Tree: TVirtualStringTree; AllowEdit: Bool
16751675
16761676destructor TDataTypeEditorLink.Destroy;
16771677begin
1678- FreeAndNil (FTreeSelect);
1679- FreeAndNil (FMemoHelp);
1678+ ReleaseEditorControl (FTreeSelect);
1679+ ReleaseEditorControl (FMemoHelp);
16801680 inherited ;
16811681end ;
16821682
0 commit comments