Recent

Author Topic: This project (with heaptrc) gives mem leaks on exit  (Read 1085 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
This project (with heaptrc) gives mem leaks on exit
« on: May 02, 2022, 07:52:07 pm »
On both Qt5 and Gtk2 the project (attached) gives lot of mem leaks, on exit. Run it on Linux Terminal.
Project has 'use heaptrc' flag cheched.
What am I doing wrong?

Code: Pascal  [Select][+][-]
  1. { TForm1 }
  2.  
  3. const
  4.   crNiceScrollNone  = TCursor(-30);
  5.   crNiceScrollUp    = TCursor(-31);
  6.   crNiceScrollDown  = TCursor(-32);
  7.   crNiceScrollLeft  = TCursor(-33);
  8.   crNiceScrollRight = TCursor(-34);
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. begin
  12.   FBmp := TPortableNetworkGraphic.Create;
  13.   FBmp.LoadFromResourceName(HInstance, 'AB_MOVE');
  14.  
  15.   Cur0:= LoadCursor(HInstance, 'AB_MOVE');
  16.   Cur1:= LoadCursor(HInstance, 'AB_MOVE_U');
  17.   Cur2:= LoadCursor(HInstance, 'AB_MOVE_D');
  18.   Cur3:= LoadCursor(HInstance, 'AB_MOVE_L');
  19.   Cur4:= LoadCursor(HInstance, 'AB_MOVE_R');
  20.  
  21.   Screen.Cursors[crNiceScrollNone]  := Cur0;
  22.   Screen.Cursors[crNiceScrollUp]    := Cur1;
  23.   Screen.Cursors[crNiceScrollDown]  := Cur2;
  24.   Screen.Cursors[crNiceScrollLeft]  := Cur3;
  25.   Screen.Cursors[crNiceScrollRight] := Cur4;
  26. end;
  27.  
  28. procedure TForm1.FormDestroy(Sender: TObject);
  29. begin
  30.   DestroyCursor(Cur4);
  31.   DestroyCursor(Cur3);
  32.   DestroyCursor(Cur2);
  33.   DestroyCursor(Cur1);
  34.   DestroyCursor(Cur0);
  35.  
  36.   FreeAndNil(FBmp);
  37. end;
  38.  

I tried to remove 'DestroyCursor' lines (LCL does it auto), still the same leaks.
« Last Edit: May 03, 2022, 09:11:55 am by AlexTP »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #1 on: May 02, 2022, 09:03:36 pm »
Hi!

All predefined cursor constants are negativ. So you can use the positiv values for your own use.

Omit the intermediate variables.

I did it that way:

Code: Pascal  [Select][+][-]
  1. const
  2. crRealHourGlass   =  1;
  3. ....
  4. Screen.Cursors[crRealHourGlass]:=LoadCursorFromLazarusResource('hourglass');
  5.  

Winni

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #2 on: May 02, 2022, 10:23:11 pm »
So if I will use positive constants, I won't see mem leaks on exit? No, I tried it now - mem leaks still here!

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #3 on: May 02, 2022, 11:08:54 pm »
The following compiles and runs without leaks.

Whether it does what you want, I don't know.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, LCLIntf;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     procedure FormCreate(Sender: TObject);
  14.     procedure FormDestroy(Sender: TObject);
  15.   private
  16.     FBmp: TPortableNetworkGraphic;
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R res/atbinhexresources.res}
  25. {$R *.lfm}
  26.  
  27. const
  28.   crNiceScrollNone  = TCursor(-30);
  29.   crNiceScrollUp    = TCursor(-31);
  30.   crNiceScrollDown  = TCursor(-32);
  31.   crNiceScrollLeft  = TCursor(-33);
  32.   crNiceScrollRight = TCursor(-34);
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   FBmp := TPortableNetworkGraphic.Create;
  37.   FBmp.LoadFromResourceName(HInstance, 'AB_MOVE');
  38.  
  39.   Screen.Cursors[crNiceScrollNone]  := LoadCursor(HInstance, 'AB_MOVE');
  40.   Screen.Cursors[crNiceScrollUp]    := LoadCursor(HINSTANCE, 'AB_MOVE_U');
  41.   Screen.Cursors[crNiceScrollDown]  := LoadCursor(HINSTANCE, 'AB_MOVE_D');
  42.   Screen.Cursors[crNiceScrollLeft]  := LoadCursor(HINSTANCE, 'AB_MOVE_L');
  43.   Screen.Cursors[crNiceScrollRight] := LoadCursor(HINSTANCE, 'AB_MOVE_R');
  44. end;
  45.  
  46. procedure TForm1.FormDestroy(Sender: TObject);
  47. begin
  48.   FreeAndNil(FBmp);
  49. end;
  50.  
  51. end.

Josh

  • Hero Member
  • *****
  • Posts: 1270
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #4 on: May 02, 2022, 11:20:01 pm »
hi

I thought the form would destroy cur1 etc on its own when exited, as they are part of the form container.
or am i wrong.?
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #5 on: May 03, 2022, 08:40:34 am »
@howardpc,
Quote
The following compiles and runs without leaks.
I cannot confirm that - still the same leaks, on Gtk2/Qt5. Make sure you checked the IDE option 'use heaptrc'.

paweld

  • Hero Member
  • *****
  • Posts: 966
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #6 on: May 03, 2022, 09:12:20 am »
@AlexTP
Quote
@howardpc,
Quote
The following compiles and runs without leaks.
I cannot confirm that - still the same leaks, on Gtk2/Qt5. Make sure you checked the IDE option 'use heaptrc'.
on Gtk2 no leaks, tetsed on Manjaro Xfce, Lazarus trunk, fpc 3.2.2
Best regards / Pozdrawiam
paweld

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #7 on: May 03, 2022, 09:16:15 am »
I thought the form would destroy cur1 etc on its own when exited, as they are part of the form container.
or am i wrong.?

You are wrong, because you can't pass an owner to LoadCursor. This means it needs to be freed manually.

AlexTP

  • Hero Member
  • *****
  • Posts: 2365
    • UVviewsoft
Re: This project (with heaptrc) gives mem leaks on exit
« Reply #8 on: May 03, 2022, 09:17:01 am »
@PascalDragon,
can you confirm these mem leaks on gtk2/qt5?

 

TinyPortal © 2005-2018