Destructor of TLazCanvas ignores FAssignedFont lazarus / 7-LCL ---------------------------------------------------------------------- lagprogramming · 2023-07-26 08:13 lcl/lazcanvas.pas has the following lines: 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 FAssignedBrush: TFPCustomBrush;FAssignedFont: TFPCustomFont;FAssignedPen: TFPCustomPen;and 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 destructor TLazCanvas.destroy;var  i: Integer;begin  for i := 0 to GraphicStateList.Count-1 do    TLazCanvasState(GraphicStateList[i]).Free;  GraphicStateList.Free;  FAssignedBrush.Free;  FAssignedPen.Free;  inherited destroy;end;    The destructor doesn't free FAssignedFont. The following patch adds a FAssignedFont.Free; line. 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 33d5d0e382..057e7fea51 100644--- a/lcl/lazcanvas.pas+++ b/lcl/lazcanvas.pas@@ -555,6 +555,7 @@ begin     TLazCanvasState(GraphicStateList[i]).Free;   GraphicStateList.Free;   FAssignedBrush.Free;+  FAssignedFont.Free;   FAssignedPen.Free;   inherited destroy; end; ---------------------------------------------------------------------- AlexTP · 2023-07-26 08:25 Posted to https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/40406 ---------------------------------------------------------------------- AlexTP · 2023-07-27 08:27 Juha Manninen commented: Do you have an example project that leaks memory because of this? ---------------------------------------------------------------------- lagprogramming · 2023-07-27 08:28 I can't provide an example. The idea is to treat FAssignedFont same as FAssignedBrush and FAssignedPen. Either all three of them should be freed, either none. Why!? Because in other places they are treated similar. 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 // These properties are utilized to implement LCLIntf.SelectObject// to keep track of which brush handle was assigned to this canvas// They are not utilized by TLazCanvas itself property AssignedPen: TFPCustomPen read GetAssignedPen write FAssignedPen;property AssignedBrush: TFPCustomBrush read GetAssignedBrush write FAssignedBrush;property AssignedFont: TFPCustomFont read GetAssignedFont write FAssignedFont;    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.GetAssignedBrush: TFPCustomBrush;begin  if FAssignedBrush = nil then    Result := TFPEmptyBrush.Create  else    Result := FAssignedBrush;end; function TLazCanvas.GetAssignedPen: TFPCustomPen;begin  if FAssignedPen = nil then    Result := TFPEmptyPen.Create  else    Result := FAssignedPen;end; function TLazCanvas.GetAssignedFont: TFPCustomFont;begin  if FAssignedFont = nil then    Result := TFPEmptyFont.Create  else    Result := FAssignedFont;end; ---------------------------------------------------------------------- wp · 2023-07-27 08:54 All this sounds reasonable. But without an example it is risky. While you immediately can use the canvas' Pen or Brush for drawing, the Font needs a special treatment: it is created outside the canvas: https://wiki.freepascal.org/fcl-image#Drawing_text. Not sure if this is good example and applies also to the FAssignedFont. But I'd be careful... Do you remember the similar case with the regions? ---------------------------------------------------------------------- lagprogramming · 2023-07-27 16:17 Quote from: wp on July 27, 2023, 10:54:17 amAll this sounds reasonable. But without an example it is risky. While you immediately can use the canvas' Pen or Brush for drawing, the Font needs a special treatment: it is created outside the canvas: https://wiki.freepascal.org/fcl-image#Drawing_text. Not sure if this is good example and applies also to the FAssignedFont. But I'd be careful... Do you remember the similar case with the regions? I don't remember, but that's ok. It's great that you remember because you have the opportunity to fix the following bugs.  1/2 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;  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 destructor TLazCanvasState.Destroy;begin  Brush.Free;  Pen.Free;  Font.Free;  inherited Destroy;end;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; 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 TLazCanvas.RestoreState(AIndex: Integer);var  lState: TLazCanvasState;begin  if AIndex < 0 then AIndex := AIndex + GraphicStateList.Count;  lState := TLazCanvasState(GraphicStateList.Items[AIndex]);  GraphicStateList.Delete(AIndex);  if lState = nil then Exit;   AssignPenData(lState.Pen);  AssignBrushData(lState.Brush);  AssignFontData(lState.Font);  BaseWindowOrg := lState.BaseWindowOrg;  WindowOrg := lState.WindowOrg;  Clipping := lState.Clipping;   lState.Free;end;Notice that SaveState and RestoreState ignore TLazCanvasState.ClipRegion. Saving/Restoring the Clipping value and ignoring the ClipRegion can't be good. 2/2 Regarding the addition of the FAssignedFont.Free line in destructor TLazCanvas.destroy, let's wait until: a) FPC issue regarding DoCopyProps routines in fcl-image package is fixed. This bug is also related to the LazCanvas comment: 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     // Utilized by LCLIntf.SelectObject and by RestoreState    // This needed to be added because Pen/Brush.Assign raises exceptionshttps://gitlab.com/freepascal.org/fpc/source/-/issues/40362 b) Somebody will do something about Saving/Restoring the Clipping value in TLazCanvas, as was written at the beginning of this reply. c) TCDWidgetSet.RectVisible always returns true, which is a bug. The function should mimic TQtWidgetSet.RectVisible, but it appears that the qt function might also be buggy when QtDC.getClipping returns false. I don't have qt installed so I can't check to see if TQtWidgetSet.RectVisible returns the same results as TGtk2WidgetSet.RectVisible. d) Fix procedure TLazCanvas.ResetCanvasState, because this procedure is expected to be very important for customdrawn. After the above four fixes, we'll see if variables have been included in TLazCanvas just as workarounds for "// Routines broken/unimplemented/incompatible in FPC", that's another comment found in the same file, regarding TLazCanvas. Looking at the code of GetAssignedBrush/Pen/Font, I'm sure there are bugs. A recommended practice is that the entity that creates or allocates a block of memory should be responsible with freeing it. TLazCanvas creates brushes, pens and fonts without keeping accountability, and also tries to free a single instance of pen and brush, ignoring completely the fonts. 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);//add to uses lazcanvas and fpcanvas and then notice the 100*3 memory leaksvar  lc:TLazCanvas;  i:integer;  fb: TFPCustomBrush;  ff: TFPCustomFont;  fp: TFPCustomPen;begin  lc := TLazCanvas.create(nil);  for i:=0 to 99 do  begin    fb := lc.AssignedBrush;    ff := lc.AssignedFont;    fp := lc.AssignedPen;  end;  lc.Free;{Using Heaptrc unit (-gh)1256 memory blocks allocated : 1610996/1612008956 memory blocks freed     : 1566196/1567208300 unfreed memory blocks : 44800True heap size : 1605632True free heap : 1496832Should be : 1503232}end;