Under android, why TThread.synchronize followed by CallInUIThread result in app crash?

Viewed 1077

I have the very simple code below that run under 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;

And i can't understand why it's crash with class segmentation fault when we click on Button1Click ?

What i know :

  • IF i remove just the TThread.synchronize it's not crash
  • IF i remove just the CallInUIThread it's not crash
  • If remove the fint := 26; it's not crash
  • IF i call TThread.synchronize + CallInUIThread outside of a destructor it's not crash

It's crash in the procedure below (when CallInUIThread finish)

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;

exactly inside

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

  inherited;
end; <== here

below the Stack Trace :

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 ??

So it's let me suppose that it's an ARC problem, but i can't understand what i did wrong ? I m under delphi Berlin

NOTE

I start to be sure at 99% that it's a ARC bug

doing like this no crash :

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;

so maybe on of the procedure try to catch a strong reference to the self Object and then destroy it at the end, but at the end the object is already destroyed because it's was in the process of destroying.

0 Answers
Related