Recent

Author Topic: EasyLazFreeType Bug?  (Read 167 times)

Tommi

  • Sr. Member
  • ****
  • Posts: 258
EasyLazFreeType Bug?
« on: June 03, 2026, 09:48:09 am »
This is my example code:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, EasyLazFreeType, TTTypes,  lazfreetype;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  aFont: TFreeTypeFont;
  Glyph: TFreeTypeGlyph;
  GlyphIndex: Integer;
  avg:double;
  TextStr:string;
  CharIdx:integer;
  AdvanceMetrics:TT_Glyph_Metrics;
begin
  TextStr:='Ciao Ciao';
  aFont := TFreeTypeFont.Create;
  try
    aFont.Name := '/home/tommaso/tmp/UbuntuSans-Italic[wdth,wght].ttf';
    //Font.Style:=[ftsBold];
    avg:=0;
    for CharIdx := 1 to Length(TextStr) do
    begin
      GlyphIndex := aFont.CharIndex[ord(TextStr[CharIdx])];
      Glyph := aFont.Glyph[GlyphIndex];
      if Assigned(Glyph) then
      begin
        if TT_Get_Glyph_Metrics(Glyph.Data, AdvanceMetrics) = 0 then
        begin
          avg:=avg+AdvanceMetrics.advance*1.8; //(AdvanceMetrics.bbox.yMax-AdvanceMetrics.bbox.yMin);
        end;
        Glyph.Free;
      end;
    end;
  finally
    //aFont.Free;
  end;
end;

end.

It works, but if I uncomment  aFont.Free; it crashes.
Moreover, if I replace TextStr:=  TextStr:='Ciao Ciao'; with TextStr:=  TextStr:='Ciao Ciao;;'; it crashes on Glyph.Free;.

Any idea? Could it a bug of EasyLazFreeType?

Thank you

wp

  • Hero Member
  • *****
  • Posts: 13550
Re: EasyLazFreeType Bug?
« Reply #1 on: June 03, 2026, 10:06:38 am »
Why do you free the Glyph? You did not create it, it belongs to the font, therefore the font will free it. So, when you free the font it will free all its glyphs - but you already had destroyed them --> crash.

Correct (no memory leak seen by heaptrc):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   aFont: TFreeTypeFont;
  4.   Glyph: TFreeTypeGlyph;
  5.   GlyphIndex: Integer;
  6.   avg:double;
  7.   TextStr:string;
  8.   CharIdx:integer;
  9.   AdvanceMetrics:TT_Glyph_Metrics;
  10. begin
  11.   TextStr:='Ciao Ciao';
  12.   aFont := TFreeTypeFont.Create;
  13.   try
  14. //    aFont.Name := '/home/tommaso/tmp/UbuntuSans-Italic[wdth,wght].ttf';
  15.     aFont.Name := 'c:\Windows\Fonts\Arial.ttf';
  16.     //Font.Style:=[ftsBold];
  17.     avg:=0;
  18.     for CharIdx := 1 to Length(TextStr) do
  19.     begin
  20.       GlyphIndex := aFont.CharIndex[ord(TextStr[CharIdx])];
  21.       Glyph := aFont.Glyph[GlyphIndex];
  22.       if Assigned(Glyph) then
  23.       begin
  24.         if TT_Get_Glyph_Metrics(Glyph.Data, AdvanceMetrics) = 0 then
  25.         begin
  26.           avg:=avg+AdvanceMetrics.advance*1.8; //(AdvanceMetrics.bbox.yMax-AdvanceMetrics.bbox.yMin);
  27.         end;
  28.         //Glyph.Free;
  29.       end;
  30.     end;
  31.   finally
  32.     aFont.Free;
  33.   end;
  34. end;

Tommi

  • Sr. Member
  • ****
  • Posts: 258
Re: EasyLazFreeType Bug?
« Reply #2 on: Today at 10:57:47 am »
Ok, now it works.

Thank you :)

 

TinyPortal © 2005-2018