Recent

Author Topic: Problems using `heaptrc`  (Read 1219 times)

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Problems using `heaptrc`
« on: September 30, 2024, 01:18:28 pm »
* Mac Mini M1
* macOS 14.6.1
* Lazarus 3.4
* FPC 3.2.2

I am trying to use `heaptrc' to check my app for memory leaks. I've set the appropriate option in `Project Options' -> `Debugging` -> `Use Heaptrc unit (check for mem-leaks (-gl))` and following the advice on the wiki, I haven't added `heaptrc` to my uses clause.

I first tried adding the following code to my main form unit...
Code: Pascal  [Select][+][-]
  1. {$DEFINE debug}    
  2. ...
  3.   {$IFDEF DEBUG}
  4.   // Assuming your build mode sets -dDEBUG in Project Options/Other when defining -gh
  5.   // This avoids interference when running a production/default build without -gh
  6.  
  7.   // Set up -gh output for the Leakview package:
  8.   if FileExists('heap.trc') then
  9.     DeleteFile('heap.trc');
  10.   SetHeapTraceOutput('heap.trc');
  11.   {$ENDIF DEBUG}
  12.  

But the compiler couldn't find `SetHeapTraceOutput`. I moved this code to my `project.lpr` file and was able to build the project. However, on running the project, I could find no signs of `heap.trc` (I did a thorough disk search). Does this mean there were no leaks in my program (unlikely) or have I done something daft to hinder the reporting of leaks?

UPDATE: I even went as far as introducing a deliberate leak...
Code: Pascal  [Select][+][-]
  1. procedure TMain.FormCreate(Sender: TObject);
  2. begin
  3.   FPresetData := TPresetData.Create;
  4.   FLeak := TStringList.Create;
  5. end;
  6.  
  7. procedure TMain.FormDestroy(Sender: TObject);
  8. begin
  9.   FPresetData.Free;
  10. end;
  11.  

No `heap.trc` was created here. What am I missing?
« Last Edit: September 30, 2024, 01:27:11 pm by carl_caulkett »
"It builds... ship it!"

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Problems using `heaptrc`
« Reply #1 on: September 30, 2024, 01:26:51 pm »
and following the advice on the wiki, I haven't added `heaptrc` to my uses clause.
That is against the advice of the wiki, or did that change somehow?
The heaptrce unit should NEVER, EVER be added to the uses clause manually.
( Some vandal editted it, I just checked, will be removed)
The only ways to use heaptrce correctly is compiling with the option -gh -gl or select the dialog items in Lazarus.
NEVER add heaptrace manually. Florian himself explained why you should not do that.

If you see heaptrc in a uses clause, you know it is wrong code.

(What happens is this: the memory manager needs to be in place at the right time, including program start-up, when you add heaptrc to the uses clause instead of best practice, you will end up with TWO memory managers and the start-up code can not be resolved,)

Sheer vandalism, the wiki WAS correct. Who did that?   >:D >:D >:D >:D
« Last Edit: September 30, 2024, 01:34:34 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Problems using `heaptrc`
« Reply #2 on: September 30, 2024, 01:29:27 pm »
Read what I said... "and following the advice on the wiki, I haven't added `heaptrc` to my uses clause."
Note the use of the word "haven't", a contraction of the words "have not" 😉
"It builds... ship it!"

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Problems using `heaptrc`
« Reply #3 on: September 30, 2024, 01:35:35 pm »
No it is not. The wiki was editted in a bad way. I will revert it.
There is nothing wrong with being blunt. At a minimum it is also honest.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Problems using `heaptrc`
« Reply #4 on: September 30, 2024, 01:45:13 pm »
Sigh!

When I looked at the Wiki, it said NOT to add `heaptrc` to my uses clause, so I DID NOT add `heaptrc` to my uses clause!

This is what the wiki looked like when I read it earlier, and indeed what it looks like now.

And to what were you referring when you said "No it is not"? I hope you were not contradicting my explanation of the use of the word "haven't"  :o
« Last Edit: September 30, 2024, 01:46:53 pm by carl_caulkett »
"It builds... ship it!"

Zvoni

  • Hero Member
  • *****
  • Posts: 2798
Re: Problems using `heaptrc`
« Reply #5 on: September 30, 2024, 01:46:52 pm »
at a guess, "heap.trc" uses the PATH of the Lazarus executable and/or reverts to a Folder inside the app-bundle
Try for TESTING an absolute path "/home/[UserName]/heap.trc"
« Last Edit: September 30, 2024, 01:51:37 pm by Zvoni »
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

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Problems using `heaptrc`
« Reply #6 on: September 30, 2024, 01:47:44 pm »
If you want to see the edits you made, you can ask.
If the problem is now solved that is fine with me, but I will still revert the edits.
There is nothing wrong with being blunt. At a minimum it is also honest.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Problems using `heaptrc`
« Reply #7 on: September 30, 2024, 01:53:03 pm »
Just to clarify, are you accusing me of knowingly making incorrect edits to the wiki? Let's see what the moderators make of that...
"It builds... ship it!"

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Problems using `heaptrc`
« Reply #8 on: September 30, 2024, 01:55:01 pm »
No, it is broadly OK:
https://wiki.freepascal.org/heaptrc#Using_Heaptrc_in_FPC

I was under the impression you did not adhere to the warning, which was ad verbatim by Florian himself.

If you still.... have problems, report back.
There is nothing wrong with being blunt. At a minimum it is also honest.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Problems using `heaptrc`
« Reply #9 on: September 30, 2024, 02:05:32 pm »
at a guess, "heap.trc" uses the PATH of the Lazarus executable and/or reverts to a Folder inside the app-bundle
Try for TESTING an absolute path "/home/[UserName]/heap.trc"

Thanks for the constructive reply  ;)

I checked inside of the Lazarus app package but I didn't see anything there. None the less I changed the code to...
Code: Pascal  [Select][+][-]
  1. const HeaptrcPath = '/Users/carlcaulkett/Code/FPC/OsmosePresets/heaptrc.txt';
  2.  
  3. begin
  4.   {$IFDEF DEBUG}
  5.   // Assuming your build mode sets -dDEBUG in Project Options/Other when defining -gh
  6.   // This avoids interference when running a production/default build without -gh
  7.  
  8.   // Set up -gh output for the Leakview package:
  9.   if FileExists(HeaptrcPath) then
  10.     DeleteFile(HeaptrcPath);
  11.   SetHeapTraceOutput(HeaptrcPath);
  12.   {$ENDIF DEBUG}
  13.  

And now I have a Heaptrc log file! Thanks 🙏

Now I have to work out how to get line number information in there  ;)
"It builds... ship it!"

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Problems using `heaptrc`
« Reply #10 on: September 30, 2024, 02:10:22 pm »
I was under the impression you did not adhere to the warning, which was ad verbatim by Florian himself.

My words were "and following the advice on the wiki, I haven't added `heaptrc` to my uses clause." which, I think you would have to agree, indicate that I DID adhere to the warning...

Anyhow, that's my last word on the subject  :o
"It builds... ship it!"

Zvoni

  • Hero Member
  • *****
  • Posts: 2798
Re: Problems using `heaptrc`
« Reply #11 on: September 30, 2024, 03:01:42 pm »
Now I have to work out how to get line number information in there  ;)
AFAIK, this has to do which debugger-backend (dbg or fpdebug) and which options (dwarf etc.) are used.
No idea. Maybe Martin will chip in here.
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

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Problems using `heaptrc`
« Reply #12 on: September 30, 2024, 05:03:16 pm »
"It builds... ship it!"

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: Problems using `heaptrc`
« Reply #13 on: September 30, 2024, 05:44:24 pm »
« Last Edit: September 30, 2024, 05:46:31 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8134
Re: Problems using `heaptrc`
« Reply #14 on: September 30, 2024, 06:23:28 pm »
and following the advice on the wiki, I haven't added `heaptrc` to my uses clause.
NEVER add heaptrace manually. Florian himself explained why you should not do that.

Thaddy, I know that you speed-read, but could you please check that you've got the right end of the stick before screaming at somebody.

In actual fact there is one situation where you have to add it manually, and that is if the first item in a uses clause is cmem in a context where strings etc. need to be shared between a program and one or more dynamically-loaded libraries. And I'd much rather not have to dig back through my mailing list record to find where I was given that advice by the FPC core team.

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

 

TinyPortal © 2005-2018