Datasnap 应用程序在一段时间后死亡

Datasnap application dies after a while

Delphi 10.3.3

到这里 Android 10(也 Android 8)。电池优化已关闭。只有当我的设备在充电器上时才会出现问题。大约 48 小时后它就死了。

我试图通过定期(8 小时后)唤醒设备来解决这个问题。今天它最后一次向 3:00 服务器发送数据,所以我以为它死了。但令我大吃一惊的是,它仍然在 08:00 醒来。不幸的是在触摸屏幕后,试图打开一些东西。 Android 强迫我关闭应用程序。

这是代码的一部分:

(请原谅我的长度,它看起来很长,但这是最低限度的,我想我会包括我使用的每个过程和功能,以便也许帮助比我更聪明的人,看看我是否泄漏内存或做一些可怕的事情,注意:如果设备不在充电器上,这个完全相同的代码在数周后不会崩溃或挂起)

procedure TMain.Timer1Timer(Sender: TObject);
var SyncTime : TDateTime;
    TransferTime : TDateTime;
begin
  if FDEviceName = 'error' then Exit;

  Timer1.Enabled:=false;

  SyncTime:=inireadDt(FilePath+'/Configuration/settings.conf','Tasks','SyncTime');

  if MinutesBetween(now,SyncTime) >= 1 then
  begin
    if isDeviceIdleMode = false then
    begin
      BatteryLevel := GetBatteryLevel;
      GetWifiStatus;

      SendDeviceStatus;

      iniwrite(FilePath+'/Configuration/settings.conf','Tasks','SyncTime',now);

      if GetBatteryCharging = 0 then WakeTime := 0;

      if GetBatteryCharging in [1,2] then inc(WakeTime);
      // to prevent app from dying we wake up device on the charger after 8 hrs
      if WakeTime = 480 then
      begin
        WakeUpNow;
        WakeTime:=0;
      end;

    end;
  end;

  Timer1.Enabled:=true;
end;

function TMain.GetBatteryLevel;
var
  Filter: JIntentFilter;
  Battery: JIntent;
begin
  Result:=-1;

  try
    Filter := TJIntentFilter.Create;
    Filter.addAction(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);
    Battery := TAndroidHelper.Context.registerReceiver(NIL, filter);

    Result := Battery.getIntExtra(StringToJString('level'), -1);
  except
    Result:=-1;
  end;
end;

function TMain.GetBatteryCharging;
var
  Filter: JIntentFilter;
  Battery: JIntent;
begin
  Result:=-1;

  try
    Filter := TJIntentFilter.Create;
    Filter.addAction(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);
    Battery := TAndroidHelper.Context.registerReceiver(NIL, filter);

    Result := Battery.getIntExtra(StringToJString('plugged'), -1);

  except
    Result:=-1;
  end;
end;

procedure TMain.WakeUpNow;
var
  PowerManager : JPowerManager;
  WakeLock : JPowerManager_WakeLock;
begin

  PowerManager := TJPowerManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.POWER_SERVICE));

  WakeLock := PowerManager.newWakeLock(TJPowerManager.JavaClass.FULL_WAKE_LOCK or TJPowerManager.JavaClass.ACQUIRE_CAUSES_WAKEUP,StringToJString('NE_PACK'));
  WakeLock.acquire;

  WakeLock.release;
  WakeLock:=nil;

end;


procedure TMain.GetWifiStatus;
var obj: JObject;
    WifiMgr : JWifiManager;
    WifiInfo : JWifiInfo;
begin
  // ACCESS_FINE_LOCATION Permission and muss ALLOW LOCATION Services on Phone
  SignalStrength := 0;
  SSID := '-';

  try
    obj := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.WIFI_SERVICE);
    WifiMgr := TJWifiManager.Wrap((obj as ILocalObject).GetObjectID);

    WifiInfo := WifiMgr.getConnectionInfo;

    case WifiInfo.getRssi of
      -49..0 : SignalStrength:=4;
      -59..-50 : SignalStrength:=3;
      -70..-60 : SignalStrength:=2;
      else SignalStrength:=1;
    end;

    SSID := JStringToString(WifiInfo.getSSID);
  except
    SignalStrength := 0;
    SSID := '-';
  end;
end;

procedure TMain.SendDeviceStatus;
var
  SQLConnection : TSQLConnection;
  Temp : TServerMethods1Client;
begin
  try
    SQLConnection := TSQLConnection.Create(nil);
    try
      SQLConnection.DriverName:='DataSnap';

      SQLConnection.Params.Clear;
      SQLConnection.Params.Add('DriverUnit=Data.DBXDataSnap');
      SQLConnection.Params.Add('DriverAssemblyLoader=Borland.Data.TDBXClientDriverLoader,Borland.Data.DbxClientDriver,Version=24.0.0.0,Culture=neutral,PublicKeyToken=91d62ebb5b0d1b1b');
      SQLConnection.Params.Add('DriverName=DataSnap');
      SQLConnection.Params.Add('HostName='+FServerIP);
      SQLConnection.Params.Add('Port='+FServerPort);
      SQLConnection.Params.Add('Filters={}');
      SQLConnection.Params.Add('CommunicationIPVersion=IP_IPv4');
      SQLConnection.Params.Add('CommunicationProtocol=tcp/ip');
      SQLConnection.Params.Add('DatasnapContext=datasnap/');
      SQLConnection.Params.Add('DSProxyPort=8888');
      SQLConnection.Params.Add('CommunicationTimeout=500');
      SQLConnection.Params.Add('ConnectTimeout=500');


      SQLConnection.Connected:=true;

      Temp := TServerMethods1Client.Create(SQLConnection.DBXConnection);
      try
        FOwner := IntToStr(WakeTime);

        Temp.DEVICE_STATUS(FDeviceName,FOwner,SSID,SoftwareVersion,SignalStrength,BatteryLevel);

      finally
        Temp.Free;
      end;

      SQLConnection.Close;

    finally
      SQLConnection.Free;
    end;
  except
    on E : Exception do
    begin
      // nothing
    end;
  end;
end;

这是上次崩溃的日志文件:

03-31 07:15:16.070 30336 30374 F libc    : Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 30374 (RenderThread), pid 30336 (rcadero.NE_PACK)
03-31 07:15:16.296 14307 14307 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-31 07:15:16.296 14307 14307 F DEBUG   : Build fingerprint: 'Zebra/TC21PG/TC21:10/10-16-10.00-QG-U14-STD-HEL-04/18:user/release-keys'
03-31 07:15:16.296 14307 14307 F DEBUG   : Revision: '0'
03-31 07:15:16.296 14307 14307 F DEBUG   : ABI: 'arm'
03-31 07:15:16.297 14307 14307 F DEBUG   : Timestamp: 2021-03-31 07:15:16+0200
03-31 07:15:16.297 14307 14307 F DEBUG   : pid: 30336, tid: 30374, name: RenderThread  >>> com.embarcadero.NE_PACK <<<
03-31 07:15:16.297 14307 14307 F DEBUG   : uid: 10227
03-31 07:15:16.297 14307 14307 F DEBUG   : signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
03-31 07:15:16.297 14307 14307 F DEBUG   : Abort message: 'terminating with uncaught exception of type std::__1::system_error: thread constructor failed: Try again'
03-31 07:15:16.297 14307 14307 F DEBUG   :     r0  00000000  r1  000076a6  r2  00000006  r3  c5d2eb20
03-31 07:15:16.297 14307 14307 F DEBUG   :     r4  c5d2eb34  r5  c5d2eb18  r6  00007680  r7  0000016b
03-31 07:15:16.297 14307 14307 F DEBUG   :     r8  c5d2eb30  r9  c5d2eb20  r10 c5d2eb50  r11 c5d2eb40
03-31 07:15:16.298 14307 14307 F DEBUG   :     ip  000076a6  sp  c5d2eaf0  lr  ef7e34fb  pc  ef7e350e
03-31 07:15:16.411 14307 14307 F DEBUG   : 
03-31 07:15:16.411 14307 14307 F DEBUG   : backtrace:
03-31 07:15:16.412 14307 14307 F DEBUG   :       #00 pc 0006050e  /apex/com.android.runtime/lib/bionic/libc.so (abort+166) (BuildId: 8549daf46b82d53f76c6449b777e63ee)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #01 pc 0003606b  /system/lib/libc++.so (abort_message+86) (BuildId: ed0f2f56655f3bf72ea2135f610dee0b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #02 pc 000361f3  /system/lib/libc++.so (demangling_terminate_handler()+178) (BuildId: ed0f2f56655f3bf72ea2135f610dee0b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #03 pc 00046837  /system/lib/libc++.so (std::__terminate(void (*)())+2) (BuildId: ed0f2f56655f3bf72ea2135f610dee0b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #04 pc 00046091  /system/lib/libc++.so (__cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception*)+12) (BuildId: ed0f2f56655f3bf72ea2135f610dee0b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #05 pc 00045ffd  /system/lib/libc++.so (__cxa_throw+72) (BuildId: ed0f2f56655f3bf72ea2135f610dee0b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #06 pc 000826cf  /system/lib/libc++.so (std::__1::__throw_system_error(int, char const*)+82) (BuildId: ed0f2f56655f3bf72ea2135f610dee0b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #07 pc 0000e65f  /system/lib/libEGL.so (android::egl_cache_t::setBlob(void const*, long, void const*, long)+230) (BuildId: 338a4d9b681c482215c94f50c9c7e1b8)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #08 pc 0000e515  /system/lib/libEGL.so (android::setBlob(void const*, long, void const*, long)+20) (BuildId: 338a4d9b681c482215c94f50c9c7e1b8)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #09 pc 001000eb  /vendor/lib/egl/libGLESv2_adreno.so (BuildId: 98c923de8a462f5fbc3bd0621f98282b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #10 pc 000945dd  /vendor/lib/egl/libGLESv2_adreno.so (BuildId: 98c923de8a462f5fbc3bd0621f98282b)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #11 pc 001cd267  /system/lib/libhwui.so (GrGLCompileAndAttachShader(GrGLContext const&, unsigned int, unsigned int, char const*, int, GrGpu::Stats*, SkSL::Program::Settings const&)+86) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #12 pc 001cc245  /system/lib/libhwui.so (GrGLProgramBuilder::compileAndAttachShaders(char const*, int, unsigned int, unsigned int, SkTDArray<unsigned int>*, SkSL::Program::Settings const&, SkSL::Program::Settings::Inputs const&)+32) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #13 pc 001cb57b  /system/lib/libhwui.so (GrGLProgramBuilder::CreateProgram(GrRenderTarget*, GrSurfaceOrigin, GrPrimitiveProcessor const&, GrTextureProxy const* const*, GrPipeline const&, GrProgramDesc*, GrGLGpu*)+866) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #14 pc 001c7957  /system/lib/libhwui.so (GrGLGpu::ProgramCache::refProgram(GrGLGpu*, GrRenderTarget*, GrSurfaceOrigin, GrPrimitiveProcessor const&, GrTextureProxy const* const*, GrPipeline const&, bool)+190) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #15 pc 001c6dfd  /system/lib/libhwui.so (GrGLGpu::flushGLState(GrRenderTarget*, GrSurfaceOrigin, GrPrimitiveProcessor const&, GrPipeline const&, GrPipeline::FixedDynamicState const*, GrPipeline::DynamicStateArrays const*, int, bool)+72) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #16 pc 001c68ff  /system/lib/libhwui.so (GrGLGpu::draw(GrRenderTarget*, GrSurfaceOrigin, GrPrimitiveProcessor const&, GrPipeline const&, GrPipeline::FixedDynamicState const*, GrPipeline::DynamicStateArrays const*, GrMesh const*, int)+86) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #17 pc 001c689f  /system/lib/libhwui.so (GrGLGpuRTCommandBuffer::onDraw(GrPrimitiveProcessor const&, GrPipeline const&, GrPipeline::FixedDynamicState const*, GrPipeline::DynamicStateArrays const*, GrMesh const*, int, SkRect const&)+30) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #18 pc 001858d3  /system/lib/libhwui.so (GrGpuRTCommandBuffer::draw(GrPrimitiveProcessor const&, GrPipeline const&, GrPipeline::FixedDynamicState const*, GrPipeline::DynamicStateArrays const*, GrMesh const*, int, SkRect const&)+166) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #19 pc 00185633  /system/lib/libhwui.so (GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(GrOp const*, SkRect const&, GrProcessorSet&&, unsigned int, GrUserStencilSettings const*)+310) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #20 pc 003d800f  /system/lib/libhwui.so (_ZN12_GLOBAL__N_121ShadowCircularRRectOp9onExecuteEP14GrOpFlushStateRK6SkRectd75113d8bcc59e80cffa524fd584b8c+50) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #21 pc 001b435f  /system/lib/libhwui.so (GrOp::execute(GrOpFlushState*, SkRect const&)+50) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #22 pc 001b41bf  /system/lib/libhwui.so (GrRenderTargetOpList::onExecute(GrOpFlushState*)+282) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.412 14307 14307 F DEBUG   :       #23 pc 001f9deb  /system/lib/libhwui.so (GrDrawingManager::flush(GrSurfaceProxy*, SkSurface::BackendSurfaceAccess, GrFlushFlags, int, GrBackendSemaphore*, void (*)(void*), void*)+1086) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #24 pc 001f9921  /system/lib/libhwui.so (GrDrawingManager::prepareSurfaceForExternalIO(GrSurfaceProxy*, SkSurface::BackendSurfaceAccess, GrFlushFlags, int, GrBackendSemaphore*, void (*)(void*), void*)+128) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #25 pc 001f97eb  /system/lib/libhwui.so (GrRenderTargetContext::prepareForExternalIO(SkSurface::BackendSurfaceAccess, GrFlushFlags, int, GrBackendSemaphore*, void (*)(void*), void*)+86) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #26 pc 001f978b  /system/lib/libhwui.so (SkGpuDevice::flush()+22) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #27 pc 001b87fd  /system/lib/libhwui.so (android::uirenderer::skiapipeline::SkiaPipeline::renderFrame(android::uirenderer::LayerUpdateQueue const&, SkRect const&, std::__1::vector<android::sp<android::uirenderer::RenderNode>, std::__1::allocator<android::sp<android::uirenderer::RenderNode>>> const&, bool, android::uirenderer::Rect const&, sk_sp<SkSurface>, SkMatrix const&)+140) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #28 pc 001b852b  /system/lib/libhwui.so (android::uirenderer::skiapipeline::SkiaOpenGLPipeline::draw(android::uirenderer::renderthread::Frame const&, SkRect const&, SkRect const&, android::uirenderer::LightGeometry const&, android::uirenderer::LayerUpdateQueue*, android::uirenderer::Rect const&, bool, android::uirenderer::LightInfo const&, std::__1::vector<android::sp<android::uirenderer::RenderNode>, std::__1::allocator<android::sp<android::uirenderer::RenderNode>>> const&, android::uirenderer::FrameInfoVisualizer*)+266) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #29 pc 001f4ff5  /system/lib/libhwui.so (android::uirenderer::renderthread::CanvasContext::draw()+236) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #30 pc 001f45c1  /system/lib/libhwui.so (android::uirenderer::renderthread::DrawFrameTask::run()+156) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #31 pc 00202789  /system/lib/libhwui.so (android::uirenderer::WorkQueue::process()+164) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #32 pc 002025e1  /system/lib/libhwui.so (android::uirenderer::renderthread::RenderThread::threadLoop()+72) (BuildId: f865daa467db141161db34e821858eaa)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #33 pc 0000da2b  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+214) (BuildId: abcd5dd14a985ca140ae57aced79ae67)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #34 pc 000aaa3b  /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+20) (BuildId: 8549daf46b82d53f76c6449b777e63ee)
03-31 07:15:16.413 14307 14307 F DEBUG   :       #35 pc 000619b3  /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+30) (BuildId: 8549daf46b82d53f76c6449b777e63ee)

非常感谢您的帮助。谢谢你的时间。

移动设备上的 IIRC 无法保证 Android/iOS 系统会让应用程序永远存在。您的应用很可能被 OS 稍后发送 SIG_ABRT 以释放资源而终止。

还有一种可能是线程过多后OOM。日志报错说明创建线程有问题

可能每个DB连接使用一个线程,线程没有释放,所以过一会就崩溃了。请删除数据库连接并重试。它可能有助于找出问题的根本原因。也尝试记录内存消耗,看看这个长期存在的应用程序是否没有泄漏。

无论如何,我宁愿使用基于 HTTPS+JSON 的 REST 服务进行应用程序通信,而不是臃肿的 DataSnap RAD 客户端。

看来我需要检查我是否处于交互模式。如果我不处于交互模式,那么我必须减少向服务器返回报告的频率。例如,15 分钟而不是 1 分钟。

我在 2021 年 3 月 31 日 14:02:40 启动了应用程序,现在是 2021 年 4 月 6 日 06:11 并且仍然是 运行 。所有其他带有匿名线程等的版本都死了。只有这个工作之间有 15 分钟的工作幸存下来。我在某处读到,如果处于打瞌睡模式,应用程序只能在 10 分钟内执行一次。但似乎想不起在哪里。也许这就是为什么它在 运行 1 分钟间隔两天后被杀死的原因。

代码如下所示:

  if isInteractive = true then
    SyncPeriod := 1
  else
    SyncPeriod := 15;

  SyncTime:=inireadDt(FilePath+'/Configuration/settings.conf','Tasks','SyncTime');

  if MinutesBetween(now,SyncTime) >= SyncPeriod then
  begin
    if isDeviceIdleMode = false then
    begin
      BatteryLevel := GetBatteryLevel;
      GetWifiStatus;

      SendDeviceStatus;

      iniwrite(FilePath+'/Configuration/settings.conf','Tasks','SyncTime',now);
    end;
  end;