Recent

Author Topic: TLazSerial: Memory leak on GetSerialPortNames  (Read 1507 times)

bpranoto

  • Full Member
  • ***
  • Posts: 103
TLazSerial: Memory leak on GetSerialPortNames
« on: March 11, 2021, 11:12:29 am »
There is a memory leak on function GetSerialPortNames on unit LazSynaSer.

In that function local variable sr:TSearchRec is closed on the end of the function:

Code: Pascal  [Select][+][-]
  1. Line 2373
  2. try
  3.     TmpPorts := '';
  4.     ScanForPorts( '/dev/rfcomm*',true);
  5.  //   ScanForPorts( '/dev/pts/*');
  6.     ScanForPorts( '/dev/ttyUSB*',true);
  7.     ScanForPorts( '/dev/ttyS*',false);
  8.     ScanForPorts( '/dev/ttyAM*',true); // for ARM board
  9.     FindClose(sr);  // <= WRONG PLACE, MOVE IT TO procedure ScanForPorts()
  10.  

This causes a memory leak, after some research I found this link https://www.freepascal.org/docs-html/rtl/sysutils/findfirst.html FindClose() should be called whenever FindFirst succeed. This means FindClose() should be placed inside the sub procedure ScanForPorts()

Code: Pascal  [Select][+][-]
  1. procedure ScanForPorts( const ThisRootStr : string; special :  boolean); // added by PDF
  2.   var theDevice : String;
  3.   var FD : Cint;
  4.   var Ser : TSerialStruct;
  5.   begin
  6.     if FindFirst( ThisRootStr, $FFFFFFFF, sr) = 0 then
  7.     begin
  8.       repeat
  9.       ......
  10.       until FindNext(sr) <> 0;
  11.       FindClose(sr); // <== FindClose() moved here
  12.     end;
  13.   end;      
  14.  

After this modification, my memory leak is gone. Attached are the patch to correct the problem.

I have also reported this problem to the github repository: https://github.com/JurassicPork/TLazSerial/issues/3


Best regards,
Bambang

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 972
Re: TLazSerial: Memory leak on GetSerialPortNames
« Reply #1 on: March 11, 2021, 03:19:27 pm »
hello,
thanks bpranoto, you are right :
Quote
Remark:      A successful FindFirst call must always be followed by a FindClose call with the same Rslt record. Failure to do so will result in memory leaks. If the findfirst call failed (i.e. returned a nonzero handle) there is no need to call FindClose.
i will modify the file lazsynaser.pas in my Tlazserial github repository  with your patch soon.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

sstvmaster

  • Full Member
  • ***
  • Posts: 246
Re: TLazSerial: Memory leak on GetSerialPortNames
« Reply #2 on: March 11, 2021, 05:10:59 pm »
@JP, don't forget to update the release version on github too, it's 0.2 at the moment.

Many Thanks for your work.

rgr Maik
Windows 10 (64 bit)
Lazarus: 2.0.12 / Trunk x32

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 972
Re: TLazSerial: Memory leak on GetSerialPortNames
« Reply #3 on: March 11, 2021, 11:22:00 pm »
hello,
@JP, don't forget to update the release version on github too, it's 0.2 at the moment.

Many Thanks for your work.

rgr Maik
version 0.5 has been tagged and is available as the latest release.

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

bpranoto

  • Full Member
  • ***
  • Posts: 103
Re: TLazSerial: Memory leak on GetSerialPortNames
« Reply #4 on: March 12, 2021, 04:27:03 am »
JP, many thanks for your good work

 

TinyPortal © 2005-2018