Segmentation fault with TidHttp in a Android Service

  

I created a Android local service in delphi that triggers a thread that creates a Tidhhp, if I stop the service through the command JavaService.stopSelf and start the service again without closing the app, a “Segmentation fault(11)” error occurs when the tidhttp component executes a “get”. if I close and reopen the app this error does not occur.
My code:

LThread := TThread.CreateAnonymousThread(
procedure
var
pagina: string;
pegar: tidhttp;
seguro: TIdSSLIOHandlerSocketOpenSSL;
compressor: TIdCompressorZLib;
begin

try
pegar := tidhttp.create(nil);
compressor := TIdCompressorZLib.create(nil);
seguro := TIdSSLIOHandlerSocketOpenSSL.create(nil);
pegar.Request.useragent :=
‘Mozilla/5.0 (Linux; Android 7.0; PLUS Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36’;
pegar.ReadTimeout := 60000;
pegar.ConnectTimeout := 60000;
pegar.HTTPOptions := [hoForceEncodeParams];
pegar.IOHandler := seguro;
pegar.compressor := compressor;
pegar.HandleRedirects := false;
pegar.Request.Accept :=
‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8’;
pegar.Request.AcceptLanguage := ‘pt-BR,en-US;q=0.8,pt;q=0.5,en;q=0.3’;
pegar.Request.ContentType := ‘application/json’;
pegar.Request.Connection := ‘keep-alive’;

pegar.Request.ContentType := ”;
pagina := pegar.Get
(‘https://meyserver.com/api/versions/count’);

finally
compressor.Free;
seguro.Free;
pegar.Free;
end;
end);
LThread.FreeOnTerminate := true;
LThread.Start;
end;

The delhi debug shows that the error occurs in the “System.Generics.Collections” unit in the function:

function TThreadList <T> .LockList: TList <T>;
begin
   TMonitor.Enter (FLock);
   Result: = FList;
end;

Some idea?

Update:

The command I use to start the service from host app is:

procedure Tfrm_principal.Button13Click(Sender: TObject);
var
LIntent: JIntent;
begin
LIntent := TJIntent.create;
LIntent.setClassName(TAndroidHelper.Activity.getBaseContext,
TAndroidHelper.StringToJString(‘com.embarcadero.services.MyTeste’));
LIntent.setAction(StringToJString(‘IniciaIntent’));
TAndroidHelper.Activity.startService(LIntent);
end;

Update 2:

My code in host app:

procedure Tfrm_principal.Button13Click(Sender: TObject);
var
LIntent: JIntent;
begin
LIntent := TJIntent.create;
LIntent.setClassName(TAndroidHelper.Activity.getBaseContext,
TAndroidHelper.StringToJString(‘com.embarcadero.services.MyAppTest’));
LIntent.setAction(StringToJString(‘StartIntent’));
TAndroidHelper.Activity.startService(LIntent);
end;

procedure Tfrm_principal.Button16Click(Sender: TObject);
var
LIntent: JIntent;
begin
LIntent := TJIntent.create;
LIntent.setClassName(TAndroidHelper.Activity.getBaseContext,
TAndroidHelper.StringToJString(‘com.embarcadero.services.MyAppTest’));
LIntent.setAction(StringToJString(‘StopIntent’));
TAndroidHelper.Activity.startService(LIntent);
end;

My code in service app:

function TDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
if Assigned(Intent) then
begin
if Intent.getAction.equalsIgnoreCase(StringToJString(‘StopIntent’)) = true
then
begin
Result := TJService.JavaClass.START_NOT_STICKY;
JavaService.stopSelf;
end
else if Intent.getAction.equalsIgnoreCase(StringToJString(‘StartIntent’)) = true
then
begin
Result := TJService.JavaClass.START_STICKY;
start_thread;
end;
end;
end;

procedure TDM.start_thread;
begin
TThread.CreateAnonymousThread(
procedure
var
pagina: string;
pegar: tidhttp;
seguro: TIdSSLIOHandlerSocketOpenSSL;
compressor: TIdCompressorZLib;
begin
pegar := tidhttp.create(nil);
compressor := TIdCompressorZLib.create(nil);
seguro := TIdSSLIOHandlerSocketOpenSSL.create(nil);
try
pegar.Request.useragent :=
‘Mozilla/5.0 (Linux; Android 7.0; PLUS Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36’;
pegar.ReadTimeout := 60000;
pegar.ConnectTimeout := 60000;
pegar.HTTPOptions := [hoForceEncodeParams];
pegar.IOHandler := seguro;
pegar.compressor := compressor;
pegar.HandleRedirects := false;
pegar.Request.Accept :=
‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8’;
pegar.Request.AcceptLanguage := ‘pt-BR,en-US;q=0.8,pt;q=0.5,en;q=0.3’;
pegar.Request.ContentType := ‘application/json’;
pegar.Request.Connection := ‘keep-alive’;
try
pagina := pegar.Get(‘https://myserver.com/api/versions/count’);
except
enviar_log(‘Connection error’);
end;
finally
compressor.Free;
seguro.Free;
pegar.Free;
end;
enviar_log(‘Exit’);
end).Start;
end;

Comments are closed.