Recent

Author Topic: TFPTimer memory leak  (Read 4096 times)

Okoba

  • Hero Member
  • *****
  • Posts: 528
TFPTimer memory leak
« on: September 24, 2018, 04:04:50 pm »
Hi,

I have a memory leak problem with TFPTimer and I think I'm missing something. It happens randomly and not always.
Tested with Lazarus 1.8.4 and FPC 3.0.4 on Windows with Debug build mode and it writes a memory leak.
Can anyone help me?
Sample is attached.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Driver := TFPTimer.Create(Self);
  4.   Driver.Interval := 33;
  5.   Driver.OnTimer := @DriveOnTimer;
  6.   Driver.Enabled := True;
  7. end;
  8.  
  9. procedure TForm1.FormDestroy(Sender: TObject);
  10. begin
  11.   Driver.Enabled := False;
  12.   Driver.Free;
  13. end;
  14.  
  15. procedure TForm1.DriveOnTimer(Sender: TObject);
  16. begin
  17.   Caption := TimeToStr(Now);
  18. end;      

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TFPTimer memory leak
« Reply #1 on: September 24, 2018, 05:07:02 pm »
No memory leaks reported here.

furious programming

  • Hero Member
  • *****
  • Posts: 852
Re: TFPTimer memory leak
« Reply #2 on: September 24, 2018, 05:56:01 pm »
I tested this example, no memory leaks. WinXP, same IDE and compiler.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #3 on: September 24, 2018, 06:23:59 pm »
Thank you very much for tests, can you test a couple of times? because it seems like a thread problem and it happens sometimes.
What version do you use?

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: TFPTimer memory leak
« Reply #4 on: September 24, 2018, 06:58:52 pm »
timers need a sufficient wait. Just add a little sleep(20) or so after timer.enabled := false;
Specialize a type, not a var.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #5 on: September 25, 2018, 07:41:08 am »
Thaddy, thanks for the hint but it didn't solve the problem.
I added "Sleep(Driver.Interval + 1);" after "Driver.Enabled := False;"

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: TFPTimer memory leak
« Reply #6 on: September 25, 2018, 07:51:00 am »
Which Windows version you are using?

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #7 on: September 25, 2018, 07:56:50 am »
Windows 10, last updated.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #8 on: September 25, 2018, 08:15:51 am »
I've just tested with FPC 3.1.1 and it happened a couple of times with it too.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TFPTimer memory leak
« Reply #9 on: September 25, 2018, 11:13:06 pm »
is it possible your timer handler event is getting re-entered while you are still in there?

 For example:
        using a Application.ProcessMessages in code that got used from the Ontimer event?

  I guess it could also be in libs that you didn't even write...

 trying putting a re-entry test on the OnTimerEvent block

 ---
   If Busy Then Exist else Busy := true;

    do your timer event code now...

   Busy := False;
 
  event code doen..
The only true wisdom is knowing you know nothing

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #10 on: September 26, 2018, 07:32:44 am »
Checked your idea jamie and didn't solve the problem.

balazsszekely

  • Guest
Re: TFPTimer memory leak
« Reply #11 on: September 26, 2018, 08:18:20 am »
The leak is easily reproducible, but if you increase the interval from 33 to 100 the leak is gone, so something fishy is happening inside TFPTimer. Anyways here is a workaround, which does not leak at my side even with a small interval:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   fptimer;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.   private
  20.     FCanClose: Boolean;
  21.     Driver: TFPTimer;
  22.     procedure DriverOnTimer(Sender: TObject);
  23.     procedure DriverOnStopTimer(Sender: TObject);
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. begin
  39.   Driver := TFPTimer.Create(Self);
  40.   Driver.Interval := 33;
  41.   Driver.OnTimer := @DriverOnTimer;
  42.   Driver.OnStopTimer := @DriverOnStopTimer;
  43.   Driver.Enabled := True;
  44. end;
  45.  
  46. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  47. begin
  48.   Driver.OnTimer := nil;
  49.   Driver.Enabled := False;  
  50.   CanClose := FCanClose;
  51. end;
  52.  
  53. procedure TForm1.FormDestroy(Sender: TObject);
  54. begin
  55.   Driver.Free;
  56. end;
  57.  
  58. procedure TForm1.DriverOnTimer(Sender: TObject);
  59. begin
  60.   Caption := TimeToStr(Now);
  61. end;
  62.  
  63. procedure TForm1.DriverOnStopTimer(Sender: TObject);
  64. begin
  65.   FCanClose := True;
  66.   Close;
  67. end;
  68.  
  69. end.
  70.  
« Last Edit: September 26, 2018, 08:29:45 am by GetMem »

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #12 on: September 26, 2018, 09:43:10 am »
Nice job GetMem, yes it solves the problem for now. I did email in the FPC mail list to have team or Michael opinion.

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: TFPTimer memory leak
« Reply #13 on: September 28, 2018, 02:33:00 pm »
@OkobaPatino
I found that in your initial version doesn't leak if your form destroy is :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormDestroy(Sender: TObject);
  2. begin
  3.   Driver.Enabled := False;
  4.   Application.ProcessMessages; // <-- add that
  5.   Driver.Free;
  6. end;
  7.  
Closing the form with Alt_F4 seems  not to leak ...
My heaptrc without the Application.ProcessMessages and closing with mouse on Close Icon is :
Quote
D:\xxxxxxxxxxxxxxxx\xxxxxxxxxxxxxxxx\Telechargements\TFPTimerTest\bin\i386-win32\win32\W10Testbds_1.exe
28.09.2018 14:31:06
Heap dump by heaptrc unit
1299 memory blocks allocated : 1678915/1682744
1297 memory blocks freed     : 1678835/1682664
2 unfreed memory blocks : 80
True heap size : 720896
True free heap : 720480
Should be : 720672
Call trace for block $0284B6F0 size 8
  $0040F606
  $004109AE
  $00553BD3 line 290 of src/fptimer.pp
  $00554002 line 523 of src/fptimer.pp
  $00553988 line 183 of src/fptimer.pp
  $005538B8 line 158 of src/fptimer.pp
  $00442879 line 43 of unit1.pas
  $0041CC5D line 939 of ../../../../fpc-laz-asus/Lazarus/laz-svn-19Trunk/lcl/include/customform.inc
Call trace for block $02861AB8 size 72 TFPTimerThread:005BEB24 | TThread:005774F4
  $0040F59B
  $00553B7F line 286 of src/fptimer.pp
  $00554002 line 523 of src/fptimer.pp
  $00553988 line 183 of src/fptimer.pp
  $005538B8 line 158 of src/fptimer.pp
  $00442879 line 43 of unit1.pas
  $0041CC5D line 939 of ../../../../fpc-laz-asus/Lazarus/laz-svn-19Trunk/lcl/include/customform.inc
  $0041B227 line 149 of ../../../../fpc-laz-asus/Lazarus/laz-svn-19Trunk/lcl/include/customform.inc

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: TFPTimer memory leak
« Reply #14 on: September 28, 2018, 04:51:13 pm »
Yes you are right BrunoK. Thanks.

 

TinyPortal © 2005-2018