Recent

Author Topic: HeapTrc not showing line numbers  (Read 2703 times)

JdeHaan

  • Full Member
  • ***
  • Posts: 126
HeapTrc not showing line numbers
« on: April 20, 2024, 09:59:27 am »
Hi,

I'm trying to find out where in my code memory leaks are created (actually, memory not freed properly).
However, the HeapTrc doesn't show the line numbers.
I followed the instructions on this page: https://www.freepascal.org/docs-html/rtl/heaptrc/usage.html
And settings are as in the attached picture.

What am I missing?

Code: Text  [Select][+][-]
  1. Call trace for block $0000000106054400 size 32
  2.   $0000000105E05BA8
  3.   $0000000105E05E9A
  4.   $0000000105E05EEE
  5.   $0000000105E0532A
  6.   $0000000105E052AB
  7.   $0000000105E053EB
  8.   $0000000105E0626B
  9.   $0000000105E061DE
  10.   $0000000105E06956
  11.   $0000000105DFC752
  12.   $0000000105DC5845
  13.   $0000000105DD520C
  14.   $00007FF8178E1366
  15. Call trace for block $0000000106054200 size 32
  16.   $0000000105E05B8D
  17.   $0000000105E05E9A
  18.   $0000000105E05EEE
  19.   $0000000105E0532A
  20.   $0000000105E052AB
  21.   $0000000105E053EB
  22.   $0000000105E0626B
  23.  

---------------------------------
LAZ+FPC trunk, MacOS Sonoma

TRon

  • Hero Member
  • *****
  • Posts: 2663
Re: HeapTrc not showing line numbers
« Reply #1 on: April 20, 2024, 10:26:30 am »
User dbannon did a lot of grunt-work some time ago (probably others went the same route even before that, I seem unable to recall) trying to figure that out and the result of that is more or less summarized in this thread.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9986
  • Debugger - SynEdit - and more
    • wiki
Re: HeapTrc not showing line numbers
« Reply #2 on: April 20, 2024, 10:40:33 am »
Quote
LAZ+FPC trunk, MacOS Sonoma

Make sure "fpc trunk" is the lastest trunk => Afaik (not sure) there were issues on Mac, but they were fixed in fpc trunk.

Try explicitly setting debug info type to dwarf.

JdeHaan

  • Full Member
  • ***
  • Posts: 126
Re: HeapTrc not showing line numbers
« Reply #3 on: April 20, 2024, 12:55:14 pm »
Thanks for the suggestion.
Updated to latest trunk, set debug info to dwarf (tested all 3), but unfortunately the result is the same.

dbannon

  • Hero Member
  • *****
  • Posts: 2847
    • tomboy-ng, a rewrite of the classic Tomboy
Re: HeapTrc not showing line numbers
« Reply #4 on: April 20, 2024, 01:17:33 pm »
For a long time, the good heaptrc data was not available on the Mac. And I am not aware that that has changed to be honest. I'd love to be proven wrong.

I do all my debugging on Linux and have sufficiently little Mac specific code for that to be enough. If you can get your app to build and run a linux machine, a VM might be the way to go. I know Virtual Box is available for the Mac but never tried it my self ....

Davo 
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

JdeHaan

  • Full Member
  • ***
  • Posts: 126
Re: HeapTrc not showing line numbers
« Reply #5 on: April 20, 2024, 10:32:24 pm »
Thanks Davo,

I'll try to install Linux on an old machine, and see if I can get it to work!

TRon

  • Hero Member
  • *****
  • Posts: 2663
Re: HeapTrc not showing line numbers
« Reply #6 on: April 21, 2024, 02:33:45 am »
For a long time, the good heaptrc data was not available on the Mac. And I am not aware that that has changed to be honest. I'd love to be proven wrong.
Report 32775 is still open so that unfortunately implies no change has occurred.

dbannon

  • Hero Member
  • *****
  • Posts: 2847
    • tomboy-ng, a rewrite of the classic Tomboy
Re: HeapTrc not showing line numbers
« Reply #7 on: April 21, 2024, 02:43:06 am »
Report 32775 is still open so that unfortunately implies no change has occurred.
[/quote]

 :(

D
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Yurgenz

  • New member
  • *
  • Posts: 7
Re: HeapTrc not showing line numbers
« Reply #8 on: April 25, 2024, 10:12:23 pm »
Windows 10. If I have addresses instead of line numbers in trc file, then run program with debugger, stop it and insert address in Address field in Assembler window. And scroll up, find in what procedure this address placed. Very informative.

TRon

  • Hero Member
  • *****
  • Posts: 2663
Re: HeapTrc not showing line numbers
« Reply #9 on: April 26, 2024, 02:02:10 am »
Windows 10. If I have addresses instead of line numbers in trc file, then run program with debugger, stop it and insert address in Address field in Assembler window. And scroll up, find in what procedure this address placed. Very informative.
Heaptrace with linenumbers is suppose to work for Windows. Are you sure your build(s) contained the options that you are suppose to use ?

Thaddy

  • Hero Member
  • *****
  • Posts: 14588
  • Sensorship about opinions does not belong here.
Re: HeapTrc not showing line numbers
« Reply #10 on: April 26, 2024, 09:56:30 am »
It can also be caused when external libraries allocate memory for - which is usually documented - Freepascal is supposed to free it.
In that case the compiler can not show line numbers, because the external library is not compiled with lineinfo.

The way to test this is simply to write a simple pascal program in which some pascal allocated memory is intentionally not free'd. If that simple program does not show line numbers, there is a bug in external code, but you should be able to free the memory. If it does show line numbers, it is probably as I described.
If it does not show line numbers it is a bug in FPC or Lazarus.
Try something like this and compile without optimization:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var a:TObject;
  3. begin
  4.   a:=TObject.Create;
  5. end.
If that does show a line number then an external library is the cause of your problem. My Mac died long ago, so can not test this.
Also note it can be an external dependency you are not aware of because it is provided by the OS.
The output should be like this:
« Last Edit: April 26, 2024, 10:10:59 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

JdeHaan

  • Full Member
  • ***
  • Posts: 126
Re: HeapTrc not showing line numbers
« Reply #11 on: April 26, 2024, 02:03:47 pm »
@Taddy, on MacOs (Sonoma, FPC trunk), your example produces:

Code: Text  [Select][+][-]
  1. 1 memory blocks allocated : 16/16
  2. 0 memory blocks freed     : 0/0
  3. 1 unfreed memory blocks : 16
  4. True heap size : 360448 (96 used in System startup)
  5. True free heap : 360128
  6. Should be : 360144
  7. Call trace for block $0000000104927100 size 16
  8.   $0000000104734C3C
  9.   $00007FF8178E1366
  10.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14588
  • Sensorship about opinions does not belong here.
Re: HeapTrc not showing line numbers
« Reply #12 on: April 26, 2024, 07:05:58 pm »
that means a bug in heaptrc for macos. And unix...
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

dbannon

  • Hero Member
  • *****
  • Posts: 2847
    • tomboy-ng, a rewrite of the classic Tomboy
Re: HeapTrc not showing line numbers
« Reply #13 on: April 27, 2024, 02:50:13 am »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var a:TObject;
  3. begin
  4.   a:=TObject.Create;
  5. end.

I am not sure thats a valid test. Heaptc does not report line numbers for memory leaks in main. Maybe thats a unix thing but its very easily worked around.

Thaddys code shows me, on my linux box where heaptrc works as expected -

Code: Bash  [Select][+][-]
  1. dbannon@dell:~/Pascal/CLI/heaptrc$ ./test1
  2. Heap dump by heaptrc unit of /home/dbannon/Pascal/CLI/heaptrc/test1
  3. 1 memory blocks allocated : 8/8
  4. 0 memory blocks freed     : 0/0
  5. 1 unfreed memory blocks : 8
  6. True heap size : 32768
  7. True free heap : 32544
  8. Should be : 32568
  9. Call trace for block $00007F92E4B8D100 size 8

However, if we move the allocation to a procedure -

Code: Bash  [Select][+][-]
  1. {$mode objfpc}
  2. var a:TObject;
  3.  
  4. procedure DoIt;
  5. begin
  6.   a:=TObject.Create;
  7. end;
  8.  
  9. begin
  10.     DoIt;
  11. end.

We do see line numbers on my Linux machine.

Code: Bash  [Select][+][-]
  1. dbannon@dell:~/Pascal/CLI/heaptrc$ ./test2
  2. Heap dump by heaptrc unit of /home/dbannon/Pascal/CLI/heaptrc/test2
  3. 1 memory blocks allocated : 8/8
  4. 0 memory blocks freed     : 0/0
  5. 1 unfreed memory blocks : 8
  6. True heap size : 32768
  7. True free heap : 32544
  8. Should be : 32568
  9. Call trace for block $00007F2EB52CE100 size 8
  10.   $00000000004010CE  main,  line 10 of test2.pas
  11. dbannon@dell:~/Pascal/CLI/heaptrc$

Note the quoted line number is that of where the DoIt procedure was called from, this is always the case. I'm sure, as Thaddy would say, this is documented somewhere. Anyway, in this specific case, the test is the problem.

However, no question that heaptrc cannot show code line number on the Mac using FPC3.2.2 ! Maybe its improved since 3.2.2 (forlorn hope), I'll try and install FPC fixes there if I get a chance this afternoon. And repeat the test using my version of Thaddy's code.

Davo

edit: removed confusing line from my code block so line numbers correspond.
« Last Edit: April 27, 2024, 03:24:24 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

dbannon

  • Hero Member
  • *****
  • Posts: 2847
    • tomboy-ng, a rewrite of the classic Tomboy
Re: HeapTrc not showing line numbers
« Reply #14 on: April 27, 2024, 03:04:02 am »
Just to labour the point a bit, still on my Linux box -

dbannon@dell:~/Pascal/CLI/heaptrc$ cat test3.pas
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. var a:TObject;
  3.  
  4. procedure DoIt3;
  5. begin
  6.   a:=TObject.Create;
  7. end;
  8.  
  9.  
  10. procedure DoIt2;
  11. begin
  12.   DoIt3;
  13. end;
  14.  
  15. procedure DoIt;
  16. begin
  17.     DoIt2;
  18. end;
  19.  
  20. begin
  21.     DoIt;
  22. end.

produces -

Code: Bash  [Select][+][-]
  1.   .....
  2.   Call trace for block $00007F1645364100 size 8
  3.   $00000000004010C9  DOIT2,  line 12 of test3.pas
  4.   $00000000004010D9  DOIT,  line 17 of test3.pas
  5.   $00000000004010EE  main,  line 21 of test3.pas

No mention of line 6. Starts from the method that called the leaking method, not the leak !

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018