Recent

Author Topic: Getting started with a service (LazDaemon)  (Read 42872 times)

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: Getting started with a service (LazDaemon)
« Reply #30 on: January 10, 2022, 12:31:32 pm »
@ngx234
Quote
but the service can not be stopped. Always in STATE=3 STOP_PENDING
Test the attached project from the following post: https://forum.lazarus.freepascal.org/index.php/topic,57754.msg429768.html#msg429768

ngx234

  • New member
  • *
  • Posts: 7
Re: Getting started with a service (LazDaemon)
« Reply #31 on: January 10, 2022, 05:26:59 pm »
@getmem

@ngx234
Quote
but the service can not be stopped. Always in STATE=3 STOP_PENDING
Test the attached project from the following post: https://forum.lazarus.freepascal.org/index.php/topic,57754.msg429768.html#msg429768

program on post #27, base on your Service.zip.  Same result

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: Getting started with a service (LazDaemon)
« Reply #32 on: January 12, 2022, 06:01:27 am »
@ngx234

Quote
program on post #27, base on your Service.zip.  Same result
Just tested, it does stop at my side.

ngx234

  • New member
  • *
  • Posts: 7
Re: Getting started with a service (LazDaemon)
« Reply #33 on: January 12, 2022, 08:34:07 am »
@getmem

I didn't make myself clear in reply #27

Yes it is working. Only one glitch: when stop LazarusService, it will stop, but there is 1 memory leak (tWorkerThread).


@ngx234

Quote
program on post #27, base on your Service.zip.  Same result
Just tested, it does stop at my side.



C:\L\_src\DaemonService\Service>type LazarusService.log
LazarusService [2022-01-10 12:22:21.310 Debug] DaemonCreate: LazarusService successfully created. --> LazarusService.exe -i
LazarusService [2022-01-10 12:23:10.956 Debug] DaemonCreate: LazarusService successfully created. --> sc start lazarusService (why twice?)



GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: Getting started with a service (LazDaemon)
« Reply #34 on: January 14, 2022, 07:00:07 am »
@ngx234

Here is the log on my side:
Quote
D:\Service>_install.bat

D:\Service>LazarusService.exe -i

D:\Service>sc start lazarusservice

SERVICE_NAME: lazarusservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 11268
        FLAGS              :

D:\Service>type log.txt
LazarusService [2022-01-14 07:46:29.878 Debug] Service successfully created.
LazarusService [2022-01-14 07:47:24.696 Debug] Service successfully created.
LazarusService [2022-01-14 07:47:24.697 Debug] Service started.
LazarusService [2022-01-14 07:47:24.698 Debug] Entering worker thread Execute event

D:\Service>type heaptrace.log
D:\Service\LazarusService.exe -i
Heap dump by heaptrc unit of D:\Service\LazarusService.exe
168 memory blocks allocated : 6242/6696
168 memory blocks freed     : 6242/6696
0 unfreed memory blocks : 0
True heap size : 491520 (112 used in System startup)
True free heap : 491408

D:\Service>sc queryex lazarusservice

SERVICE_NAME: lazarusservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 11268
        FLAGS              :

D:\Service>sc stop lazarusservice

SERVICE_NAME: lazarusservice
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

D:\Service>type log.txt
LazarusService [2022-01-14 07:46:29.878 Debug] Service successfully created.
LazarusService [2022-01-14 07:47:24.696 Debug] Service successfully created.
LazarusService [2022-01-14 07:47:24.697 Debug] Service started.
LazarusService [2022-01-14 07:47:24.698 Debug] Entering worker thread Execute event
LazarusService [2022-01-14 07:50:08.783 Debug] Leaving worker thread Execute event.
LazarusService [2022-01-14 07:50:08.783 Debug] Woker thread terminated.
LazarusService [2022-01-14 07:50:09.288 Debug] Service stopped.

D:\Service>type heaptrace.log
D:\Service\LazarusService.exe -i
Heap dump by heaptrc unit of D:\Service\LazarusService.exe
168 memory blocks allocated : 6242/6696
168 memory blocks freed     : 6242/6696
0 unfreed memory blocks : 0
True heap size : 491520 (112 used in System startup)
True free heap : 491408

D:\Service>

As you can see: "0 unfreed memory blocks : 0", so no leaks. Are you sure you tested the attached service from the link?


PS: I just noticed that in your log is also "0 unfreed memory blocks : 0". You are not leaking,  everything is OK.   :)
« Last Edit: January 14, 2022, 07:07:39 am by GetMem »

ngx234

  • New member
  • *
  • Posts: 7
Re: Getting started with a service (LazDaemon)
« Reply #35 on: January 14, 2022, 09:51:50 am »
hi Getmem

I test again your service.zip. Just add, use heaptrc unit in project option. And add 1 line :
heaptrc.SetHeapTraceOutput(ChangeFileExt(ParamStr(0), '.heaptrc.txt'));

I can stop the service, but 52 bytes mem leak  :-X

C:\L\_src\DaemonService\ServiceGetmemOriginal>type LazarusService.heaptrc.txt
C:\L\_src\DaemonService\ServiceGetmemOriginal\LazarusService.exe -i
Heap dump by heaptrc unit of C:\L\_src\DaemonService\ServiceGetmemOriginal\LazarusService.exe
172 memory blocks allocated : 7153/7616
172 memory blocks freed     : 7153/7616
0 unfreed memory blocks : 0
True heap size : 524288 (144 used in System startup)
True free heap : 524144
C:\L\_src\DaemonService\ServiceGetmemOriginal\LazarusService.exe --run
Heap dump by heaptrc unit of C:\L\_src\DaemonService\ServiceGetmemOriginal\LazarusService.exe
218 memory blocks allocated : 8527/9168
217 memory blocks freed     : 8475/9112
1 unfreed memory blocks : 52

True heap size : 524288 (144 used in System startup)
True free heap : 524144
Should be : 523992
Call trace for block $06B500D8 size 52
  $00401B61
  $00431FC1


 :-\

Few seconds after :  sc stop lazarusservice
Please check ->    : sc query lazarusservice

Or please attach your last source code, I will compile and test it  :D

Win 10 x64, My IDE:
Lazarus 2.2.0 (rev lazarus_2_2_0-10-g991a5bbf25)
FPC 3.2.3 i386-win32-win32/win64

Lazarus 2.2.0 (rev lazarus_2_2_0-10-g991a5bbf25)
FPC 3.3.1 i386-win32-win32/win64

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: Getting started with a service (LazDaemon)
« Reply #36 on: January 14, 2022, 11:01:30 am »
I attached the source. Still no memory leak at my side. I'm working with Lazarus Trunk/FPC 3.2.2.

ngx234

  • New member
  • *
  • Posts: 7
Re: Getting started with a service (LazDaemon)
« Reply #37 on: January 15, 2022, 05:48:50 am »
I attached the source. Still no memory leak at my side. I'm working with Lazarus Trunk/FPC 3.2.2.

Thanks getmem, there is no mem leak in your example.

The culprit is in my code

Code: Pascal  [Select][+][-]
  1. var ss : string;
  2. begin//main program LazarusService;
  3.   RegisterDaemonClass(TDaemon);
  4.   RegisterDaemonMapper(TDaemonMapper);
  5.  
  6.   //SetHeapTraceOutput(ChangeFileExt(ParamStr(0),'.txt')); //mem leak on stop service!!!
  7.  
  8.   //ss:=ParamStr(0)+'.txt'; SetHeapTraceOutput(ss);        //also mem leak on stop service!!!
  9.  
  10.   ss:=ParamStr(0)+'.txt';                                  //no mem leak
  11.  
  12.   SetHeapTraceOutput('heaptrc.txt');                       //no mem leak
  13.  
  14.   GlobalSkipIfNoLeaks:=True;
  15.  
  16.   Application.Title := 'Lazarus Service Application title';
  17.   Application.EventLog.LogType := ltFile;
  18.   Application.EventLog.DefaultEventType := etDebug;
  19.   Application.EventLog.AppendContent := true;
  20.   //Application.EventLog.FileName := ExtractFilePath(Application.ExeName) + '.log';
  21.   Application.EventLog.FileName := ChangeFileExt(ParamStr(0), '.log');  //no mem leak
  22.   Application.Initialize;
  23.   Application.Run;
  24. end.//file
  25.  
  26. {
  27. test.bat==================begin
  28. del      *.txt     *.log
  29. pause    sc delete lazarusService
  30. sc delete lazarusService
  31. pause    lazarusService.exe -i
  32. lazarusService.exe -i
  33. sc queryex lazarusService
  34. pause    sc start lazarusService
  35. sc start lazarusService
  36. sc queryex lazarusService
  37. pause    sc stop lazarusService
  38. sc stop lazarusService
  39. pause    sc queryex lazarusService
  40. sc queryex lazarusService
  41. pause    sc delete lazarusService
  42. sc delete lazarusService
  43. type   *.txt   *.log
  44. test.bat==================end
  45. }



« Last Edit: January 15, 2022, 06:02:01 am by ngx234 »

 

TinyPortal © 2005-2018