先看下面的FMX.Layouts.pas中一段代碼
procedure TCustomScrollBox.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Single);
begin
FMouseEvents := True;
inherited;
if (Button = TMouseButton.mbLeft) then
begin
MousePosToAni(X, Y);
AniMouseDown(ssTouch in Shift, X, Y);
end;
end;
在執行Inherited;這行時可能會調用控件的OnDblClick事件,如果此時在OnDblClick中將Form或控件釋放了,后面調用MousePosToAni可能就會造成內存訪問異常
因此最好能夠在UI線程(主線程)中執行MouseDown完全后,再調用Form或控件的釋放,如下面
procedure TForm1.OnListBox1Item1DblClick(Sender:TObject);
begin
....//處理一些事情
AsyncCallInUIThread(
procedure
begin
Self.DisposeOf; //延遲釋放,防止內存訪問異常
end);
end;
下面是AsyncCallInUIThread的實現:
procedure AsyncCallInUIThread(Proc: TProc);
begin
TThread.CreateAnonymousThread(
procedure
begin
Sleep(0);
TThread.Synchronize(nil,
procedure
begin
Proc;
end);
end).Start;
end;
