Recent

Author Topic: Using Heaptrace with Linux  (Read 1841 times)

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 605
Using Heaptrace with Linux
« on: February 10, 2025, 05:15:22 am »
I'm using Lazarus 3.2 on linux.  In https://wiki.freepascal.org/heaptrc I find the following:

Quote
Using Heaptrc in Lazarus

To enable this in your Lazarus project:

    Go to Project Options/Linking and
    in the Debugging section enable Use Heaptrc unit (check for mem-leaks) (-gh)

You can test for the presence of Heaptrc in a similar manner as in the above example by testing for the presence of UseHeapTrace in your Project.lpr file.

Can anyone tell me where to look for Project Options/Linking ?  I've been looking on and off all day and just can't find it:  probably there and I'm just not seeing it.

Thanks.

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: Using Heaptrace with Linux
« Reply #1 on: February 10, 2025, 06:32:23 am »
menu->project->project-options
in left tree at compiler options select debugging
at right page under section "other debugging info" check "Use Heaptrc unit (check for memleaks) (-gh)"

While you're at it also check the option "display line numbers in runtime-error backtraces (-gl)"
Today is tomorrow's yesterday.

dbannon

  • Hero Member
  • *****
  • Posts: 3298
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Using Heaptrace with Linux
« Reply #2 on: February 10, 2025, 08:17:37 am »
Curt, thanks for flagging that out of date wiki page, I have given it a bit of a tweak.

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

TRon

  • Hero Member
  • *****
  • Posts: 4157
Re: Using Heaptrace with Linux
« Reply #3 on: February 10, 2025, 11:37:35 am »
Thank you for the tweak dbannon 👍
Today is tomorrow's yesterday.

Zvoni

  • Hero Member
  • *****
  • Posts: 2914
Re: Using Heaptrace with Linux
« Reply #4 on: February 10, 2025, 11:44:38 am »
Also be careful if you use another memory-manager (e.g. cmem)
See last paragraph "Why Heaptrc should not be added to the uses clause manually" on the Wiki
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: Using Heaptrace with Linux
« Reply #5 on: February 10, 2025, 11:46:55 am »
Also be careful if you use another memory-manager (e.g. cmem)
See last paragraph "Why Heaptrc should not be added to the uses clause manually" on the Wiki

In my experience with dynamic linkage on Linux which mandates cmem, if you need HeapTrace for debugging then if /does/ have to be added manually but carefully positioned second (i.e. after cmem which is first).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 1967
    • http://www.cdbc.dk
Re: Using Heaptrace with Linux
« Reply #6 on: February 10, 2025, 11:49:52 am »
Hi
Mark, I put it third, after 'cthreads', are there any problems in that?
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: Using Heaptrace with Linux
« Reply #7 on: February 10, 2025, 11:59:14 am »
Hi
Mark, I put it third, after 'cthreads', are there any problems in that?
Regards Benny

Let me take a look at a fairly big project. One moment please...

Code: Pascal  [Select][+][-]
  1. (* Note manual addition of cmem below, this is required to allow strings and    *)
  2. (* objects to be shared/transferred between the main program and a shared       *)
  3. (* library. Note also relative position of HeapTrc, if cmem is used it is not   *)
  4. (* possible to specify this at the project level (i.e. to use FPC's -gh option).*)
  5.  
  6. uses
  7. {$ifdef USE_CMEM }
  8.   cmem, {$ifdef USE_HEAPTRC } HeapTrc, {$endif USE_HEAPTRC }
  9. {$endif USE_CMEM }
  10.   {$IFDEF UNIX}
  11.   cwstring,
  12.   {$IFDEF USE_CTHREADS}
  13.   cthreads,
  14.   {$ENDIF}{$ENDIF}
  15.   Classes, SysUtils, ...
  16.  

I wouldn't have thought it would be significant, since threads are much more a runtime thing. I do recall the presence of cthreads in some other project being important in some non-obvious way... I really can't remember the detail though and even though I've almost certainly written it up in comments etc. I really don't know where to start looking.

All I can say is that what I've got there works for me, and is probably a useful fallback for anybody in case of non-obvious problems.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 1967
    • http://www.cdbc.dk
Re: Using Heaptrace with Linux
« Reply #8 on: February 10, 2025, 12:16:10 pm »
Hi
@Mark: Thanks mate, for checking...
I too will leave it as it is, since I'm not experiencing trouble, because I /always/ use threads and only once in a while 'heaptrace'.
If and when that fails, I can always come back and copy your approach  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

dbannon

  • Hero Member
  • *****
  • Posts: 3298
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Using Heaptrace with Linux
« Reply #9 on: February 10, 2025, 12:39:23 pm »
Mine typically looks like this -

Code: Pascal  [Select][+][-]
  1.     {$DEFINE UseCThreads}
  2.     {$IFDEF UNIX}{$IFDEF UseCThreads}
  3.    { cmem,} cthreads,
  4.     {$ENDIF}{$ENDIF}  .....  
 

cmem at the start !  When I decide its time to do some heaptrc checking, I manually comment out cmem (as shown), obviously I loose much of the the benefit of threading but its only during debugging so who cares. So far, never had a release go out with cmem disabled !

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

cdbc

  • Hero Member
  • *****
  • Posts: 1967
    • http://www.cdbc.dk
Re: Using Heaptrace with Linux
« Reply #10 on: February 10, 2025, 12:47:22 pm »
Hi Davo
I read it, like you're /always/ using 'cmem' instead of fpc-mem-man?!?
I'm aware of certain situations, where regarding threads the cmem is faster, but always?!?
...and ofc with libraries cmem is a certainty. { though I have fpcmemman too}
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: Using Heaptrace with Linux
« Reply #11 on: February 10, 2025, 01:14:35 pm »
In the case of the project I quoted: it's very heavily oriented towards multiple threads for various subsystems, and I'm always using cmem because it can dynamically link a library to dump a (large) file for logic analysis or (potentially) to attach a non-standard peripheral. I'm fairly confident in its memory and thread management because multiple threads can output state info through the library simultaneously.

I was going to muse on whether, since the order is significant, both cmem and heaptrace insertion should ideally be done by the compiler. However since I'm talking about on-demand .so library loading rather than at-startup, this is probably outside what the compiler/linker could be expected to handle even if hinted by a commandline option.

What we obviously /don't/ want however is for either the compiler or the Lazarus IDE to try to rearrange things: I've already had situations where "LineInfo" was automagically rewritten to "lnfodwrf" which didn't help when I was trying to write my code to detect and report what format of debugging was being used.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

cdbc

  • Hero Member
  • *****
  • Posts: 1967
    • http://www.cdbc.dk
Re: Using Heaptrace with Linux
« Reply #12 on: February 10, 2025, 03:10:14 pm »
Hi
Pheeewww,
Quote
I've already had situations where "LineInfo" was automagically rewritten to "lnfodwrf"
the above, I've yet to experience...  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: Using Heaptrace with Linux
« Reply #13 on: February 19, 2025, 08:42:16 pm »
I do recall the presence of cthreads in some other project being important in some non-obvious way... I really can't remember the detail though and even though I've almost certainly written it up in comments etc. I really don't know where to start looking.

Apologies for generating noise, but I've just found a note on this and thought it worth passing on.

Basically, I can't remember the symptoms or quite how I got to the fix, but the .lpr of a dynamically-linkable library called by multiple threads (protected by syncobjs in the caller) /had/ to import cthreads to work properly.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dbannon

  • Hero Member
  • *****
  • Posts: 3298
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Using Heaptrace with Linux
« Reply #14 on: February 20, 2025, 03:09:33 am »
Hi Davo
I read it, like you're /always/ using 'cmem' instead of fpc-mem-man?!?
I'm aware of certain situations, where regarding threads the cmem is faster, but always?!?
...and ofc with libraries cmem is a certainty. { though I have fpcmemman too}
Regards Benny

Yeah Benny, sorry for late answer, some family issues at the moment.

cmem (and cthreads ?) is only a thing on Unix, probably does not even compile on Windows ? It makes a big difference with multithreaded code (I benchmark stuff like that). Not significant in an all single thread code (but not tested by me).

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