Recent

Author Topic: How to set Heaptrc in linux  (Read 1578 times)

jianwt

  • Jr. Member
  • **
  • Posts: 66
How to set Heaptrc in linux
« on: February 02, 2023, 02:21:49 am »
(lazarus 2.2.4 fpc 3.2.2 for linux64)
How to set Heaptrc in linux? Thank you very much!

Novice, please tell me the detailed steps of how to use,
https://wiki.lazarus.freepascal.org/heaptrc
Yes, but I still don't know how to use Heaptrc on linux. Set up lazarus IDE as provided on the site, but Heaptrc does not work when used.
How to use it, looking forward to reply!!

Are there any other testing tools or methods?


Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   SysUtils,
  13.   Interfaces, // this includes the LCL widgetset
  14.   Forms, Unit1
  15.   { you can add units after this };
  16.  
  17. {$R *.res}
  18.  
  19. begin
  20.   {$if declared(UseHeapTrace)}
  21.   GlobalSkipIfNoLeaks := True; // supported as of debugger version 3.2.0
  22.   {$endIf}
  23.   RequireDerivedFormResource:=True;
  24.   Application.Scaled:=True;
  25.   Application.Initialize;
  26.   Application.CreateForm(TForm1, Form1);
  27.   Application.Run;
  28. end.
  29.  
  30.  
« Last Edit: February 02, 2023, 02:29:32 am by jianwt »

PierceNg

  • Sr. Member
  • ****
  • Posts: 334
    • SamadhiWeb
Re: How to set Heaptrc in linux
« Reply #1 on: February 02, 2023, 03:54:11 am »
https://wiki.lazarus.freepascal.org/heaptrc

Quote
On *nix (including BSD, Linux and macOS), by default, nothing will be shown for GUI programs. See leakview for details on how to make use of Heaptrc effectively.

jianwt

  • Jr. Member
  • **
  • Posts: 66
Re: How to set Heaptrc in linux
« Reply #2 on: February 02, 2023, 04:02:04 am »
@PierceNg
I checked the website. I didn't get it.
Can you tell me how to turn on this display function in linux?
Thank you so much!!

Blaazen

  • Hero Member
  • *****
  • Posts: 3160
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to set Heaptrc in linux
« Reply #3 on: February 02, 2023, 04:06:03 am »
If you open Console In/Output (Ctrl+Alt+O or View -> Debug Window -> ...) you can see output when you terminate your program (GUI or non-GUI).

Code: Pascal  [Select][+][-]
  1. Heap dump by heaptrc unit of "/media/homeM/v1/Projects/Components/ShortArray/lib/x86_64-linux/project1"
  2. 14239 memory blocks allocated : 1803444/1832336
  3. 14239 memory blocks freed     : 1803444/1832336
  4. 0 unfreed memory blocks : 0
  5. True heap size : 622592
  6. True free heap : 622592
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jianwt

  • Jr. Member
  • **
  • Posts: 66
Re: How to set Heaptrc in linux
« Reply #4 on: February 02, 2023, 04:30:36 am »
In linux, I want to debug the code for any incorrect code that does not release memory. The following code clearly creates the code that does not release memory. I want to detect similar problems in the program, but by pressing the following Settings, I cannot find and locate the unit code that has the problem.



Routine code:

Code: Pascal  [Select][+][-]
  1.  
  2. {$IFDEF DEBUG}
  3.   // Assuming your build mode sets -dDEBUG in Project Options/Other when defining -gh
  4.   // This avoids interference when running a production/default build without -gh
  5.  
  6.   // Set up -gh output for the Leakview package:
  7.   if FileExists('heap.trc') then
  8.     DeleteFile('heap.trc');
  9.   SetHeapTraceOutput('heap.trc');
  10.   {$ENDIF DEBUG}
  11.  
  12. //////////////////////////////////////////////////////
  13. procedure TForm1.FormCreate(Sender: TObject);
  14. var
  15.   slst: TStringList;
  16. begin
  17.   slst := TStringList.Create;
  18.   slst.Add('AAAAA');
  19.   //slst.Free;
  20. end;  
  21.  

How to detect errors!

TRon

  • Hero Member
  • *****
  • Posts: 834
Re: How to set Heaptrc in linux
« Reply #5 on: February 02, 2023, 06:25:19 am »
How to detect errors!
That is a very good question jianwt because I agree with you that the trace that is produced by heaptrace does not help you a bit.

Even stronger, it works confusing for newbies and might even suggest to them that there is a leak somewhere in the Lazarus/Free Pascal internals (which it isn't but how would someone not knowing know).

There is no trace (phun intended) whatsoever of your Main form/unit other than that there is a leak detected in project1 (duh!  :) )

Thaddy

  • Hero Member
  • *****
  • Posts: 12933
Re: How to set Heaptrc in linux
« Reply #6 on: February 02, 2023, 07:56:17 am »
If both -gl and -gh are defined (or -glh) you should get
1. the unit that leaks
2. the line number of the offending allocation that is leaked
3. the leak size


It does not matter if it is windows or linux
« Last Edit: February 02, 2023, 07:59:16 am by Thaddy »
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

TRon

  • Hero Member
  • *****
  • Posts: 834
Re: How to set Heaptrc in linux
« Reply #7 on: February 02, 2023, 08:03:02 am »
@Thaddy:
agreed.

Alas, instead the trace looks like:
Code: [Select]
/media/ramdisk/projects/project1
Heap dump by heaptrc unit of /media/ramdisk/projects/project1
729 memory blocks allocated : 1540130/1540872
727 memory blocks freed     : 1539922/1540664
2 unfreed memory blocks : 208
True heap size : 1671168
True free heap : 1670528
Should be : 1670576
Call trace for block $00007FFFF6A0ED20 size 64
  $0000000000450C07  DOCREATE,  line 921 of include/customform.inc
  $000000000044E8C7  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  $0000000000457964  CREATE,  line 3225 of include/customform.inc
  $0000000000462515  CREATEFORM,  line 2176 of include/application.inc
  $000000000041F16F  main,  line 26 of project1.lpr
  $00000000004540B8  CREATE,  line 2040 of include/customform.inc
  $0000000000457939  CREATE,  line 3224 of include/customform.inc
  $0000000000462515  CREATEFORM,  line 2176 of include/application.inc
  $000000000041F16F  main,  line 26 of project1.lpr
Call trace for block $00007FFFF67A8D60 size 144
  $0000000000450C07  DOCREATE,  line 921 of include/customform.inc
  $000000000044E8C7  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  $0000000000457964  CREATE,  line 3225 of include/customform.inc
  $0000000000462515  CREATEFORM,  line 2176 of include/application.inc
  $000000000041F16F  main,  line 26 of project1.lpr

And in case using leakview it will open up mentioned files when clicking on a message.

Blaazen

  • Hero Member
  • *****
  • Posts: 3160
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to set Heaptrc in linux
« Reply #8 on: February 02, 2023, 04:09:40 pm »
If you modify code like this:
Code: Pascal  [Select][+][-]
  1. { TForm1 }
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   Test;     // this is actually line 35
  6. end;
  7.  
  8. procedure TForm1.Test;
  9.  var lst: TStringList;
  10.  begin
  11.    lst := TStringList.Create;
  12.    lst.Add('AAAAA');
  13. end;
  14.  
  15. end.
you get this output
Code: Pascal  [Select][+][-]
  1. Call trace for block $00007FFFED4FD2C0 size 64
  2.   $0000000000474CB9  FORMCREATE,  line 35 of unit1.pas
  3.   $0000000000458FCF  DOCREATE,  line 921 of include/customform.inc
  4.   $00000000004561FE  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  5.   $00000000004619B3  CREATE,  line 3204 of include/customform.inc
  6.   $000000000046FADC  CREATEFORM,  line 2198 of include/application.inc
  7.   $000000000042420E  main,  line 22 of project1.lpr
  8. Call trace for block $00007FFFF7F76EC0 size 144
  9.   $0000000000474CB9  FORMCREATE,  line 35 of unit1.pas
  10.   $0000000000458FCF  DOCREATE,  line 921 of include/customform.inc
  11.   $00000000004561FE  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  12.   $00000000004619B3  CREATE,  line 3204 of include/customform.inc
  13.   $000000000046FADC  CREATEFORM,  line 2198 of include/application.inc
  14.   $000000000042420E  main,  line 22 of project1.lpr
Heaptrace is one step behind.
I just don't know if it was always like this.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 12933
Re: How to set Heaptrc in linux
« Reply #9 on: February 02, 2023, 04:59:11 pm »
afaik it was always like this because the trace chokes on the final step. But heaptrc should be correct on where it chokes. heaptrc and trace or call stack are two different things.
« Last Edit: February 02, 2023, 07:30:18 pm by Thaddy »
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

jianwt

  • Jr. Member
  • **
  • Posts: 66
Re: How to set Heaptrc in linux
« Reply #10 on: February 03, 2023, 01:32:13 am »
@Blaazen
Thank you very much for your reply.
“Call trace for block $00007FFFED4FD2C0 size 64
  $0000000000474CB9  FORMCREATE,  line 35 of unit1.pas
  $0000000000458FCF  DOCREATE,  line 921 of include/customform.inc
  $00000000004561FE  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  $00000000004619B3  CREATE,  line 3204 of include/customform.inc
  $000000000046FADC  CREATEFORM,  line 2198 of include/application.inc
  $000000000042420E  main,  line 22 of project1.lpr
Call trace for block $00007FFFF7F76EC0 size 144
  $0000000000474CB9  FORMCREATE,  line 35 of unit1.pas
  $0000000000458FCF  DOCREATE,  line 921 of include/customform.inc
  $00000000004561FE  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  $00000000004619B3  CREATE,  line 3204 of include/customform.inc
  $000000000046FADC  CREATEFORM,  line 2198 of include/application.inc
  $000000000042420E  main,  line 22 of project1.lpr”

In linux system, how lazarus has such a debugging prompt? In linux system, I can not debug out, please give me a lot of advice.

Blaazen

  • Hero Member
  • *****
  • Posts: 3160
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to set Heaptrc in linux
« Reply #11 on: February 03, 2023, 02:25:00 am »
I wrote it above, if you open Console In/Output (Ctrl+Alt+O or View -> Debug Window -> Console) you can see there output when you terminate your program (GUI or non-GUI). You must use -gh (or the checkbox as in your screenshot in your very first post).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jianwt

  • Jr. Member
  • **
  • Posts: 66
Re: How to set Heaptrc in linux
« Reply #12 on: February 03, 2023, 04:38:45 am »
@Blaazen   in linux??
In the debugging of my application, the console does not display "//slst.Free; No release error message!!

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. var
  4.   slst: TStringList;
  5. begin
  6.   slst := TStringList.Create;
  7.   slst.Add('AAAAA');
  8.   //slst.Free;
  9. end;  
  10.  
  11.  
  12. 725 memory blocks freed     : 1539082/1539776
  13. 2 unfreed memory blocks : 208
  14. True heap size : 1671168
  15. True free heap : 1670528
  16. Should be : 1670576
  17. Call trace for block $00007F388BFCBD20 size 64
  18.   $0000000000450BE7  DOCREATE,  line 921 of include/customform.inc
  19.   $000000000044E8A7  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  20.   $0000000000457944  CREATE,  line 3225 of include/customform.inc
  21.   $00000000004624F5  CREATEFORM,  line 2176 of include/application.inc
  22.   $000000000041F148  $main,  line 35 of project1.lpr
  23.   $0000000000454098  CREATE,  line 2040 of include/customform.inc
  24.   $0000000000457919  CREATE,  line 3224 of include/customform.inc
  25.   $00000000004624F5  CREATEFORM,  line 2176 of include/application.inc
  26.   $000000000041F148  $main,  line 35 of project1.lpr
  27. Call trace for block $00007F388DF9DEC0 size 144
  28.   $0000000000450BE7  DOCREATE,  line 921 of include/customform.inc
  29.   $000000000044E8A7  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  30.   $0000000000457944  CREATE,  line 3225 of include/customform.inc
  31.   $00000000004624F5  CREATEFORM,  line 2176 of include/application.inc
  32.   $000000000041F148  $main,  line 35 of project1.lpr
  33.  
« Last Edit: February 03, 2023, 04:42:40 am by jianwt »

af0815

  • Hero Member
  • *****
  • Posts: 1167
Re: How to set Heaptrc in linux
« Reply #13 on: February 03, 2023, 08:01:08 am »
@Blaazen   in linux??
In the debugging of my application, the console does not display "//slst.Free; No release error message!!

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. var
  4.   slst: TStringList;
  5. begin
  6.   slst := TStringList.Create;
  7.   slst.Add('AAAAA');
  8.   //slst.Free;
  9. end;  
  10.  
  11.  
  12. 2 unfreed memory blocks : 208
  13.  
//slist.free can never shown, because it is a comment. And you get the information you have lost 2 blocks and a hint where it can be. Not more not less. heaptrace cannot analyse your code, it can only give some hints, yes it is not a simple tool and you have to learn how to read the output. This is a very simple sample, normal the output is more complex.
regards
Andreas

jianwt

  • Jr. Member
  • **
  • Posts: 66
Re: How to set Heaptrc in linux
« Reply #14 on: February 03, 2023, 08:18:40 am »
@af0815

in linux:
725 memory blocks freed     : 1539082/1539776
2 unfreed memory blocks : 208
True heap size : 1671168
True free heap : 1670528
Should be : 1670576
Call trace for block $00007F388BFCBD20 size 64
  $0000000000450BE7  DOCREATE,  line 921 of include/customform.inc
  $000000000044E8A7  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  $0000000000457944  CREATE,  line 3225 of include/customform.inc
  $00000000004624F5  CREATEFORM,  line 2176 of include/application.inc
  $000000000041F148  $main,  line 35 of project1.lpr
  $0000000000454098  CREATE,  line 2040 of include/customform.inc
  $0000000000457919  CREATE,  line 3224 of include/customform.inc
  $00000000004624F5  CREATEFORM,  line 2176 of include/application.inc
  $000000000041F148  $main,  line 35 of project1.lpr
Call trace for block $00007F388DF9DEC0 size 144
  $0000000000450BE7  DOCREATE,  line 921 of include/customform.inc
  $000000000044E8A7  AFTERCONSTRUCTION,  line 77 of include/customform.inc
  $0000000000457944  CREATE,  line 3225 of include/customform.inc
  $00000000004624F5  CREATEFORM,  line 2176 of include/application.inc
  $000000000041F148  $main,  line 35 of project1.lpr



However, under windows, specific error units can be received.
There is a big gap between the two.
« Last Edit: February 03, 2023, 09:17:05 am by jianwt »

 

TinyPortal © 2005-2018