Daniel Mazur
Daniel Mazur

Reputation: 83

Delphi 10.3.1 - Android Service hangs on System.InitUnits

I've created simple Android Service on 10.2.3 and pinned it to my Android App same as it stands in docs. However, there where no libProxyAndroidService.so in {$BDS}/lib/android/release, I've copied it from debug dir. Next thorn made by Embarcadero to me was hanging of whole application while calling

TLocalServiceConnection.StartService('somename');

I have installed 10.3.1 with hope that this bug is eliminated in this release, but it did the same. Running app in debug mode, I have putted some breakpoints in System.Android.ServiceApplication, when steping over and over by code, it crashed in System.InitUnits, line 23357:

 try
    while I < Count do
    begin
      P := Table^[I].Init;
      Inc(I);
      InitContext.InitCount := I;
      if Assigned(P) and Assigned(Pointer(P^)) then
      begin
{$IF defined(MSWINDOWS)}
        TProc(P)();
{$ELSEIF (defined(POSIX) and defined(CPUX86)) and defined(ASSEMBLER)}
        CallProc(P, InitContext.Module^.GOT);
{$ELSE}
        TProc(P)(); << 23357 crashing 
{$ENDIF}
      end;

enter image description here After execution of malfunctional P, UI thread hangs out, Service is never executed, but in background Android App is still executing code (new threads in message log)


Edit: I've checked what is under P^ This is initalization part of unit FMX.Platform

Upvotes: 2

Views: 1140

Answers (1)

Daniel Mazur
Daniel Mazur

Reputation: 83

https://quality.embarcadero.com/browse/RSP-17857 It's old bug, never fixed by Embarcadero Just remove all things which use FMX.Types unit, remove this unit from evey uses. Then set ClassGroup to TPersistent
Wasted hours :|

procedure TPlatformAndroid.BindAppGlueEvents;
var
  AndroidAppGlue: TAndroidApplicationGlue;
begin
  AndroidAppGlue := PANativeActivity(System.DelphiActivity)^.instance; // <------- Error occurs here
  AndroidAppGlue.OnApplicationCommandEvent := HandleApplicationCommandEvent;
  AndroidAppGlue.OnContentRectEvent := HandleContentRectChanged;
  AndroidAppGlue.OnInputEvent := HandleAndroidInputEvent;
end;

Related issues: RSP-12199 and RSP-13381. FMX seems to have a lot of problems related to use of System.DelphiActivity in a service. And for good reason. DelphiActivity probably shouldn't exist in the first place! You are not supposed to hold on to references to the Activity object in the first place. And a Service doesn't even require an Activity to run! Apps and Services alike run as Contexts instead (the Activity and Service classes both derive from the Context class), so if you need to hold a reference to something, hold one to the Context that the code is running in (which FMX also does). What is FMX doing with DelphiActivity that is so important that it can't be done in other (safer) ways?

Conclusion: There is nil in System.DelphiActivity in Services so loading FMX units will create a crash in initUnits. PDFed link with bug explanation: https://www.docdroid.net/TfUjBwg/bug.pdf

Upvotes: 3

Related Questions