Heaptrc/memory leak question lazarus / 21-General ---------------------------------------------------------------------- jbmckim · 2021-02-10 23:17 I wrote a "Split" procedure to parse strings.  Heaptrc says it's leaking.  This is the innermost block of the leak report: 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 Split(Delimiter: Char; Str: string;var ListOfStrings: TStrings) ;  begin      ListOfStrings.Clear;     ListOfStrings.Delimiter       := Delimiter;     ListOfStrings.StrictDelimiter := True;     ListOfStrings.DelimitedText   := Str;   end;                                                                       This is called from here which is where the TStrings that are passed in are created and disposed. 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  function SpecNumToSerialNum(SpecNum : integer) : string;  var     i : integer;     strRtn : String;     ListOfStrings: TStrings;     begin      ListOfStrings := TStringList.Create;      for i := 0 to (SpecSerialStrLst.Count - 1) do      begin        if (i = SpecNum) then           begin             Split('=',SpecSerialStrLst[i],ListOfStrings);             strRtn := ListOfStrings[1];           end;       end;     SpecNumToSerialNum := strRtn;     ListOfStrings.Clear;    ListOfStrings.Free;     end;                 Is it the var parameter in the 'Split' routine that's leaking?  It seems like the compiler would clean that when the program is torn down.  Any ideas on how to fix this? Thanks ---------------------------------------------------------------------- lucamar · 2021-02-11 02:46 It might well be the var and it's not needed anyway, since you use the passed instance as-is rather than (re)create a new one. Try declaring it as: 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 Split(Delimiter: Char; Str: string;    const ListOfStrings: TStrings); ---------------------------------------------------------------------- Zvoni · 2021-02-11 07:25 Why not as a Function instead of a Procedure? Like the FindAllFiles-Function --> https://wiki.freepascal.org/FindAllFiles ---------------------------------------------------------------------- lucamar · 2021-02-11 09:54 Quote from: Zvoni on February 11, 2021, 08:25:28 amWhy not as a Function instead of a Procedure? As a procedure it has the advantage that you can declare the parameter as TStrings, allowing you to pass to it any descendant. Since it's an abstract class, you can't use it as a function result so you'd have to use one of its descendants (e.g. TStringList), which might somehow limit its utility. That's why routines such as FindAllFiles() are usually implemented as a couple of overloads: a function returning a TStringList and a procedure with a TStrings parameter. ---------------------------------------------------------------------- jbmckim · 2021-02-11 17:57 That didn't fix it but that is an interesting facet I didn't know about. I wonder if HeapTrc actually looks at the heap in this case or if HeapTrc is already gone at the point this is cleaned as part of take down.  Of course, if HT is checking/marking load time vs unload states, then it is catching this. I've found these constructs to be quite finicky in this regard.  If it is a problem, I may need to "roll my own" Split procedure. lucamar, you're right about the procedure being less fussy re parameters (speaking generally).  I got hung up on this awhile back and converted this (and a couple others) to procedures for this reason. ---------------------------------------------------------------------- lucamar · 2021-02-11 19:07 There is another error that I didn't notice before; in SpecNumToSerialNum() you declare ListOfStrings as TStrings. You should declare it as TStringList, which is its real class. I'm not entirely sure whether that might lead to a leak but it's worth correcting and testing if it does the trick There's also the superfluous:Code: [Select]ListOfStrings.Clear;It's completely unnecessary to do that just before you Free the list but it has nothing to do with the presumed leak. All in all, I'd simplify it 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 function SpecNumToSerialNum(SpecNum : integer) : string;var  strRtn : String = '';  ListOfStrings: TStringList;begin  if (SpecNum >= 0) and (SpecNum < SpecSerialStrLst.Count) then    try      ListOfStrings := TStringList.Create;      Split('=',SpecSerialStrLst[SpecNum],ListOfStrings);      strRtn := ListOfStrings[1];    finally      ListOfStrings.Free;    end;  SpecNumToSerialNum := strRtn;end; ETA Forgot about this: Quote from: jbmckim on February 11, 2021, 06:57:56 pmI wonder if HeapTrc actually looks at the heap in this case or if HeapTrc is already gone at the point this is cleaned as part of take down. HeapTrc is the last in the chain of finalizations, which is (among other reasons) why one shouldn't add it to the uses clause but let the compiler add it on its own. What HeapTrc does is, more or less and simplifying enormously, implement a custom memory manager, so it is the first initialized, to build up the heap, and the last finalized to tear it down. Everything else is created/freed between those two stages.