Problems using `heaptrc` lazarus / 12-Debugger ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 11:18 * 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$DEFINE debug}    ...  {$IFDEF DEBUG}  // Assuming your build mode sets -dDEBUG in Project Options/Other when defining -gh  // This avoids interference when running a production/default build without -gh   // Set up -gh output for the Leakview package:  if FileExists('heap.trc') then    DeleteFile('heap.trc');  SetHeapTraceOutput('heap.trc');  {$ENDIF DEBUG}  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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMain.FormCreate(Sender: TObject);begin  FPresetData := TPresetData.Create;  FLeak := TStringList.Create;end; procedure TMain.FormDestroy(Sender: TObject);begin  FPresetData.Free;end;  No `heap.trc` was created here. What am I missing? ---------------------------------------------------------------------- Thaddy · 2024-09-30 11:26 Quote from: carl_caulkett on September 30, 2024, 01:18:28 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? ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 11:29 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" 😉 ---------------------------------------------------------------------- Thaddy · 2024-09-30 11:35 No it is not. The wiki was editted in a bad way. I will revert it. ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 11:45 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" ---------------------------------------------------------------------- Zvoni · 2024-09-30 11:46 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" ---------------------------------------------------------------------- Thaddy · 2024-09-30 11:47 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. ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 11:53 Just to clarify, are you accusing me of knowingly making incorrect edits to the wiki? Let's see what the moderators make of that... ---------------------------------------------------------------------- Thaddy · 2024-09-30 11:55 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. ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 12:05 Quote from: Zvoni on September 30, 2024, 01:46:52 pmat 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const HeaptrcPath = '/Users/carlcaulkett/Code/FPC/OsmosePresets/heaptrc.txt'; begin  {$IFDEF DEBUG}  // Assuming your build mode sets -dDEBUG in Project Options/Other when defining -gh  // This avoids interference when running a production/default build without -gh   // Set up -gh output for the Leakview package:  if FileExists(HeaptrcPath) then    DeleteFile(HeaptrcPath);  SetHeapTraceOutput(HeaptrcPath);  {$ENDIF DEBUG}  And now I have a Heaptrc log file! Thanks 🙏 Now I have to work out how to get line number information in there ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 12:10 Quote from: Thaddy on September 30, 2024, 01:55:01 pmI 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 ---------------------------------------------------------------------- Zvoni · 2024-09-30 13:01 Quote from: carl_caulkett on September 30, 2024, 02:05:32 pmNow 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. ---------------------------------------------------------------------- carl_caulkett · 2024-09-30 15:03 Somebody on a different thread posted the answer https://forum.lazarus .freepascal.org/index.php/topic,67035.msg531595.html#msg531595 ---------------------------------------------------------------------- Thaddy · 2024-09-30 15:44 The correct answer was in the unmutilated wiki. Will be repaired, given time. https://www.freepascal.org/docs-html/rtl/heaptrc/index.html#:~:text=Th e%20HEAPTRC%20unit%20can%20be%20used%20to%20debug ---------------------------------------------------------------------- MarkMLl · 2024-09-30 16:23 Quote from: Thaddy on September 30, 2024, 01:26:51 pmQuote from: carl_caulkett on September 30, 2024, 01:18: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