Memory Leak in Fcl-Passrc wiki code lazarus / 31-LCL ---------------------------------------------------------------------- simone · 2020-04-20 15:23 I seems to me that the code in the wiki of fcl-passrc (https://wiki.freepascal.org/fcl-passrc) has some memory leaks that happens when the parsed module has syntax errors.  This can be revealed with the following code, slightly modified in order to show memory leaks: 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 {$mode objfpc}{$H+} uses SysUtils, Classes, PParser, PasTree, HeapTrc; type  { We have to override abstract TPasTreeContainer methods.    See utils/fpdoc/dglobals.pp for an implementation of TFPDocEngine,    a "real" engine. }  TSimpleEngine = class(TPasTreeContainer)  public    function CreateElement(AClass: TPTreeElement; const AName: String;    AParent: TPasElement; AVisibility: TPasMemberVisibility;      const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;    override;    function FindElement(const AName: String): TPasElement; override;  end; function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;  AParent: TPasElement; AVisibility: TPasMemberVisibility;  const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;begin  Result := AClass.Create(AName, AParent);  Result.Visibility := AVisibility;  Result.SourceFilename := ASourceFilename;  Result.SourceLinenumber := ASourceLinenumber;end; function TSimpleEngine.FindElement(const AName: String): TPasElement;begin  { dummy implementation, see TFPDocEngine.FindElement for a real example }  Result := nil;end; var  M: TPasModule;  E: TPasTreeContainer;  I: Integer;  Decls: {$ifdef VER2_6} TList {$else} { for FPC > 2.6.0 } TFPList {$endif};begin  if FileExists('heap.trc') then    DeleteFile('heap.trc');  SetHeapTraceOutput('heap.trc');   E := TSimpleEngine.Create;  try    M := ParseSource(E, 'WrongUnit.pas', 'linux', 'i386');     { Cool, we successfully parsed the module.      Now output some info about it. }    if M.InterfaceSection <> nil then    begin      Decls := M.InterfaceSection.Declarations;      for I := 0 to Decls.Count - 1 do        Writeln('Interface item ', I, ': ' +          (TObject(Decls[I]) as TPasElement).Name);    end else      Writeln('No interface section --- this is not a unit, this is a ', M.ClassName);     if M.ImplementationSection <> nil then // may be nil in case of a simple program    begin      Decls := M.ImplementationSection.Declarations;      for I := 0 to Decls.Count - 1 do        Writeln('Implementation item ', I, ': ' +          (TObject(Decls[I]) as TPasElement).Name);    end;     FreeAndNil(M);  finally FreeAndNil(E) end;  readln;end The example in the wiki seems correct to me. I suspect that the memory leaks are in then fcl-passrc library. Can anyone confirm or deny this? Thank in advance. ---------------------------------------------------------------------- jamie · 2020-04-21 00:28 I can't say, did you supply the file you hard coded in there ? Also I see no destructor to clean up things .. Maybe the M.Free should be moved in the finally section incase code after that is failing and M := Nil at the start of the code just in case the Constructor fails ---------------------------------------------------------------------- zamronypj · 2020-04-21 01:01 Quote from: simone on April 20, 2020, 05:23:57 pm 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 ...  E := TSimpleEngine.Create;  try    M := ParseSource(E, 'WrongUnit.pas', 'linux', 'i386');    ...    FreeAndNil(M);  finally FreeAndNil(E) end;  ...  Does change above 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   E := TSimpleEngine.Create;  try    M := ParseSource(E, 'WrongUnit.pas', 'linux', 'i386');    try        ...    finally        FreeAndNil(M);  end;  finally        FreeAndNil(E)   end;...  (as suggested by jamie) fix the leak? ---------------------------------------------------------------------- simone · 2020-04-21 07:53 Quote from: jamie on April 21, 2020, 02:28:27 amI can't say, did you supply the file you hard coded in there? The hardcoded file can be whatever source with syntax error that has to be parsed. ---------------------------------------------------------------------- Thaddy · 2020-04-21 08:13 It is not the best example anyway, with that careless use of FreeAndNil.... ---------------------------------------------------------------------- simone · 2020-04-21 08:55 Quote from: jamie on April 21, 2020, 02:28:27 amAlso I see no destructor to clean up things .. Yes. TSimpleEngine is derived from TPasTreeContainer defined in PParser unit of fcl-passrc, which also does not have a defined constructor. Quote from: zamronypj on April 21, 2020, 03:01:40 am(as suggested by jamie) fix the leak? The memory leaks persist... Quote from: Thaddy on April 21, 2020, 10:13:14 amIt is not the best example anyway, with that careless use of FreeAndNil.... I agree with you. I have not found better examples. To understand the use of fcl-passrc I'm studying the code.