[SOLVED] Freeing at destructor TLazCanvasState.Destroy; lazarus / 31-LCL ---------------------------------------------------------------------- lagprogramming · 2023-03-28 18:19 lcl/lazcanvas.pas contains destructor TLazCanvasState.Destroy; Shouldn't Font also be freed there!? 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 if Font <> nil then Font.Free; Here is existing code: 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 TLazCanvasState = class  public    Brush: TFPCustomBrush;    Pen: TFPCustomPen;    Font: TFPCustomFont;    BaseWindowOrg: TPoint;    WindowOrg: TPoint;    Clipping: Boolean;    ClipRegion: TFPCustomRegion;    destructor Destroy; override;  end;  destructor TLazCanvasState.Destroy;begin  if Brush <> nil then Brush.Free;  if Pen <> nil then Pen.Free;  inherited Destroy;end; ---------------------------------------------------------------------- Blaazen · 2023-03-28 21:34 Probably should. A basic test gives memory 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 unit Unit1;{$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, LazCanvas, FPImage; type  { TForm1 }  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var lc: TLazCanvas;    fpi: TFPCustomImage;begin  fpi:=TFPCustomImage.Create(100, 100);  lc:=TLazCanvas.create(fpi);  lc.SaveState;  lc.RestoreState(0);  lc.Free;  fpi.Free;end; end.I don't know what is the purpose of LazCanvas, I never used it. ---------------------------------------------------------------------- dsiders · 2023-03-28 21:45 Quote from: Blaazen on March 28, 2023, 11:34:05 pmProbably should. A basic test gives memory 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 unit Unit1;{$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, LazCanvas, FPImage; type  { TForm1 }  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var lc: TLazCanvas;    fpi: TFPCustomImage;begin  fpi:=TFPCustomImage.Create(100, 100);  lc:=TLazCanvas.create(fpi);  lc.SaveState;  lc.RestoreState(0);  lc.Free;  fpi.Free;end; end.I don't know what is the purpose of LazCanvas, I never used it. It is used as the DC in custom drawn widgetsets. ---------------------------------------------------------------------- jamie · 2023-03-28 21:48 Rather badly written. You don't need to test for Nil if you are going to use Free because Free does that already. also, its possible maybe a shared font is being used so it could be a bad idea to free it there. ---------------------------------------------------------------------- Blaazen · 2023-03-28 22:01 Quotealso, its possible maybe a shared font is being used so it could be a bad idea to free it there. No. It creates a new instance of Font and copies all parameters: 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 TLazCanvas.SaveState: Integer;var  lState: TLazCanvasState;begin  lState := TLazCanvasState.Create;   lState.Brush := Brush.CopyBrush;  lState.Pen := Pen.CopyPen;  lState.Font := Font.CopyFont;  lState.BaseWindowOrg := BaseWindowOrg;  lState.WindowOrg := WindowOrg;  lState.Clipping := Clipping;   Result := GraphicStateList.Add(lState);end; function TFPCustomFont.CopyFont : TFPCustomFont;begin  result := TFPCustomFont(self.ClassType.Create);  result.DoCopyProps (self);end; CustomDrawn are simply *alpha*. ---------------------------------------------------------------------- jamie · 2023-03-28 22:59 Does it leak ? ---------------------------------------------------------------------- wp · 2023-03-28 23:28 Yes. And there is another related leak because the GraphicStateList, a TFPList, does not free its elements. Committed a fix to Laz/main. A further leak probably is due to FAssignedFont which is auto-created at first access (GetAssignedFont), but never destroyed. However, I did not touch it ATM because I am not 100% sure about font handling in TLazCanvas which probably should get the fonts via FreeType. ---------------------------------------------------------------------- lagprogramming · 2023-03-29 12:01 Quote from: wp on March 29, 2023, 01:28:43 amYes. And there is another related leak because the GraphicStateList, a TFPList, does not free its elements. Committed a fix to Laz/main. A further leak probably is due to FAssignedFont which is auto-created at first access (GetAssignedFont), but never destroyed. However, I did not touch it ATM because I am not 100% sure about font handling in TLazCanvas which probably should get the fonts via FreeType. Indeed, destructor TLazCanvas.destroy; also needs an FAssignedFont.Free; line. This is the patch. Even if the change is minor, it helps clarify what's the deal with having these FAssigned* variables. Maybe they can be completely replaced with parent Font, Brush and Pen. Looking at both lazcanvas.pas and customdrawn code it's hard to believe they are used properly. 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 diff --git a/lcl/lazcanvas.pas b/lcl/lazcanvas.pasindex bcc4d0cc17..8cbbea89cf 100644--- a/lcl/lazcanvas.pas+++ b/lcl/lazcanvas.pas@@ -549,6 +549,7 @@ begin   GraphicStateList.Free;   FAssignedBrush.Free;   FAssignedPen.Free;+  FAssignedFont.Free;   inherited destroy; end; ---------------------------------------------------------------------- wp · 2023-03-29 15:54 Do you have a working example in which TLazCanvas is used to draw text? All I know is code in which FreeType is employed, and this does not even need a canvas. 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, ExtCtrls, EasyLazFreeType; type   { TForm1 }   TForm1 = class(TForm)    Image1: TImage;    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    FFont: TFreeTypeFont;    procedure DrawOnIntfImage;   public   end; var  Form1: TForm1; implementation {$R *.lfm} uses  fpImage, fpCanvas,  IntfGraphics, LazFreeTypeIntfDrawer, LazFreeTypeFontCollection; procedure TForm1.DrawOnIntfImage;const  FontFile = 'arial.ttf';var  bmp: TBitmap;  img: TLazIntfImage;  drawer: TIntfFreeTypeDrawer;begin  bmp := TBitmap.Create;  try    bmp.PixelFormat := pf32Bit;    bmp.SetSize(Image1.Width, Image1.Height);     img := bmp.CreateIntfImage;    try      drawer := TIntfFreeTypeDrawer.Create(img);      try        if FFont = nil then      begin          FFont := TFreeTypeFont.Create;          FFont.Name := FontCollection.AddFile('c:\windows\fonts\' + FontFile).Family.FamilyName;        end;        FFont.SizeInPoints := 24;        drawer.DrawTextRect('This is a test', FFont, 0,0, img.Width, img.Height, colRed, [ftaCenter, ftaVerticalCenter]);      finally        drawer.Free;      end;      bmp.LoadFromIntfImage(img);      Image1.Picture.Assign(bmp);    finally      img.Free;    end;  finally    bmp.Free;  end;end; procedure TForm1.FormCreate(Sender: TObject);begin  DrawOnIntfImage;end; procedure TForm1.FormDestroy(Sender: TObject);begin  FreeAndNil(FFont);end; ---------------------------------------------------------------------- lagprogramming · 2023-03-29 17:26 Maybe this is helpful. Add a break-point at function TCDWidgetSet.ExtTextOut. It contains lDestCanvas: TLazCanvas absolute DC; This bug can be noticed in Linux-CustomDrawn. Probably Windows too. The first saved bitmap is not good, the second one looks OK. 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 TForm1.Button1Click(Sender: TObject);{BUG!Press the button once and notice that h1<>h2. h1 should equal h2!Press the button twice and notice that h1=h2.Look at the saved bitmap files.In linux-customdrawn the first returned TextHeight value is different than the following TextHeight values that appear when you press the button.}var  h1,h2: integer;  b: tbitmap;begin  b:=tbitmap.Create;  b.Canvas.Font.Name := 'Arial';  b.Canvas.Font.Height := 19; //Changing this value will not change the first computed value of h1!!!!!  b.Canvas.Font.Color:=clred;  b.SetSize(b.Canvas.TextWidth('Test'),b.Canvas.TextHeight('Test'));  h1:=b.Canvas.TextHeight('Test');  {BUG TRIGGERED BY THE FOLLOWING CANVAS.TEXTOUT LINE! Notice that h2 becomes different than h1!  Commenting "ExtUTF8Out(FHandle, X, Y, Flags, nil, PChar(Text), Length(Text), nil);" in procedure TCanvas.TextOut(X,Y: Integer; const Text: String); leaves the value unchanged to 15!}  b.Canvas.TextOut(0, 0, 'Test');  h2:=b.Canvas.TextHeight('Test');  Form1.Caption:=Form1.Caption+' h1='+inttostr(h1)+' h2='+inttostr(h2);  {Notice that the first bitmap saved shows only a part of the "Test" text!   Pressing the button twice will make the bitmap show the entire "Test" text.}  b.SaveToFile('/tmp/bmp.bmp');  b.Free;end; ---------------------------------------------------------------------- wp · 2023-03-29 21:33 Lots of strange things going on here. For example: Why is Font.PixelsPerInch = 0 on Windows? ---------------------------------------------------------------------- lagprogramming · 2023-03-31 17:48 Quote from: wp on March 29, 2023, 11:33:01 pmLots of strange things going on here. For example: Why is Font.PixelsPerInch = 0 on Windows? In Linux applying the following patch fixes the TextHeight problem. It returns the same value, no matter how many times you press the button. I wonder if it fixes the bug in Windows, too. 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 diff --git a/lcl/interfaces/customdrawn/customdrawnwinapi.inc b/lcl/interfaces/customdrawn/customdrawnwinapi.incindex 3843bd1cef..412a42a1ca 100644--- a/lcl/interfaces/customdrawn/customdrawnwinapi.inc+++ b/lcl/interfaces/customdrawn/customdrawnwinapi.inc@@ -528,7 +528,7 @@ begin   );*)    lFont.Size := Abs(LogFont.lfHeight);-+  lFont.ftFont.SizeInPoints := lFont.Size; (*  // Some values at available on Qt documentation at a table   // Others are guesses. The best would be to test different values for those   // See: http://doc.trolltech.com/4.1/qfont.html#Weight-enum ---------------------------------------------------------------------- wp · 2023-03-31 18:03 Thanks, seems to fix the issue in Windows, too. Applied.