在android下,为什么TThread.synchronize后跟CallInUIThread导致应用程序崩溃?

时间:2017-06-03 23:22:53

标签: delphi delphi-10.1-berlin

我在Android下运行以下非常简单的代码:

type

  TmyObject = class(Tobject)
  public
    fint: integer;
    destructor Destroy; override;
  end;

destructor TmyObject.Destroy;
begin

  TThread.synchronize(nil,
    procedure
    begin
      fint := 26;
    end);

  CallInUIThread(
    procedure
    begin
    end);

  inherited;

end;

procedure TForm1.Button1Click(Sender: TObject);
var aObject: TmyObject;
begin
  aObject := TmyObject.Create;
  aObject.free;
end;

当我们点击Button1Click时,我无法理解为什么它会因类分段错误而崩溃?

我所知道的:

  • 如果我只移除TThread.synchronize它没有崩溃
  • 如果我只移除CallInUIThread它没有崩溃
  • 如果删除fint := 26;它没有崩溃
  • 如果我在析构函数之外调用TThread.synchronize + CallInUIThread,则不会崩溃

在下面的程序中崩溃(当CallInUIThread完成时)

function _InstClear(var Dest: TObject): Pointer;
{$IFDEF PUREPASCAL}
var
  P: Pointer;
begin
  Result := @Dest;
  if Dest <> nil then
  begin
    P := Pointer(Dest);
    Pointer(Dest) := nil;
    TObject(P).__ObjRelease;
  end;
end;

完全在里面

destructor TJavaLocal.Destroy;
begin
  TJNIResolver.DeleteGlobalRef(FObjectID);

  inherited;
end; <== here
堆栈跟踪下面的

System._InstClear(@0xa042672c: nil)
System._FinalizeArray(0xa042672c,0xa33c1af8,1)
System._FinalizeRecord(0xa0426720,0xa33c1c80)
System.TObject.CleanupInstance(0xa0426720)
System.TObject.FreeInstance(0xa0426720)
System._ClassDestroy(0xa0426720)
System.TObject.~TObject(0xa0426720,true)
System.TObject.__ObjRelease(0xa0426720)
System.TInterfacedObject._Release(0xa0426720)
:A231CC0E __stub_in16s__ZN6System17TInterfacedObject8_ReleaseEv24
System._IntfClear(@0xa0676cbc: nil)
System._FinalizeArray(0xa0676cbc,0xa313e480,1)
System._FinalizeRecord(0xa0676c98,0xa313e55c)
System.TObject.CleanupInstance(0xa0676c98)
System.TObject.FreeInstance(0xa0676c98)
System._ClassDestroy(0xa0676c98)
Androidapi.Jnibridge.TJavaLocal.~TJavaLocal(0xa0676c98,true)
System.TObject.__ObjRelease(0xa0676c98)
System._InstClear(@0xbe9c1b50: nil)
Androidapi.Jnibridge.dispatchToNative(0xb4857c00,0xbe9c1b9c,0xbe9c1ba0,0xbe9c1ba4,2691132568)
:A3A033EC ??
:A3A033EC ??

所以,我想它是一个ARC问题,但我无法理解我做错了什么?我在德尔福柏林下

注意

我开始确定99%它是一个ARC错误

这样做没有崩溃:

type

  TmyObject = class(Tobject)
  public
    fint: integer;
    destructor Destroy; override;
    procedure AnProcedure;
  end;

procedure TmyObject.AnProcedure;
begin

end;

destructor TmyObject.Destroy;
begin

  TThread.synchronize(nil,
    procedure
    begin
      fint := 26;
    end);

  CallInUIThread(AnProcedure);

  inherited;

end;

procedure TForm1.Button1Click(Sender: TObject);
var aObject: TmyObject;
begin
  aObject := TmyObject.Create;
  aObject.free;
end;

所以也许程序尝试捕获对自身对象的强引用,然后在最后销毁它,但最后对象已经被销毁,因为它正在销毁。< / p>

0 个答案:

没有答案
相关问题