bgrabitmap / bgracontrols

🆗 BGRA Controls is a set of graphical UI elements that you can use with Lazarus LCL applications.
https://bgrabitmap.github.io/bgracontrols/
188 stars 32 forks source link

Memory leak with TBGRASVGImageList #134

Closed circular17 closed 1 year ago

circular17 commented 1 year ago

Redirected from https://github.com/bgrabitmap/lazpaint/issues/535

It is expected to have memory leaks with XML though there may be a bit more with this component. Here is log with lazpaint:

[roland@localhost x86_64-linux-gtk2-3.2.2.nosync]$ ./lazpaint /home/roland/Images/eschecs.png
Heap dump by heaptrc unit of /home/roland/Applications/lazpaint-7.2.2/lazpaint/debug/x86_64-linux-gtk2-3.2.2.nosync/lazpaint
280722 memory blocks allocated : 62028013/62642928
280708 memory blocks freed     : 62022017/62636920
14 unfreed memory blocks : 5996
True heap size : 3047424
True free heap : 3038272
Should be : 3038728
Call trace for block $00007FC2B65EC600 size 38
  $0000000000C9A4E8
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B65EC500 size 32
  $0000000000C9A4E8
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B6594E30 size 3080
  $0000000000B393B8
  $0000000000C9A4E8
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B65EC400 size 40
  $0000000000B393B8
  $0000000000C9A4E8
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B668F400 size 84
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B65EC300 size 32
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B668F2C0 size 98
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B65EC200 size 32
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B668F180 size 96
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
Call trace for block $00007FC2B65EC100 size 40
  $0000000000B38CFE
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B6594550 size 2048
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B65EC000 size 32
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2B66BAE80 size 128
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
Call trace for block $00007FC2BCEAD0C0 size 216
  $0000000000C99575  DEFINEPROPERTIES,  line 189 of bgrasvgimagelist.pas
  $00000000006AB949
  $00000000006ABA04
  $00000000006AD42F
  $0000000000852FB4  INITLAZRESOURCECOMPONENT,  line 3180 of lresources.pp
  $000000000084B330  INITRESOURCECOMPONENT,  line 798 of lresources.pp
  $000000000045A4CB  PROCESSRESOURCE,  line 2052 of include/customform.inc
  $000000000045A374  CREATE,  line 2040 of include/customform.inc
  $000000000045D88F  CREATE,  line 3224 of include/customform.inc
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
  $000000000042134C  main,  line 216 of lazpaint.lpr
  $00000000004678F1  CREATEFORM,  line 2176 of include/application.inc
  $00000000004B7D6F  FORMSNEEDED,  line 514 of lazpaintinstance.pas
  $00000000004BDEB6  PROCESSCOMMANDLINE,  line 1568 of lazpaintinstance.pas
[roland@localhost x86_64-linux-gtk2-3.2.2.nosync]$ 
lainz commented 1 year ago

Hi, I've added test_svg_theme project (so we can test the memory leak easily). And I've fixed a memory leak for bcfont in bgrasvgtheme. The other bug is in bgrasvgimagelist, ReadData (line 110) and DefineProperties (line 189).

lainz commented 1 year ago

The problem is that in the Lazarus bugtracker they will not accept this demo I made as test of the memory leak, because it requires bgrabitmap and bgracontrols.

circular17 commented 1 year ago

I have the impression that the memory leak is limited to TXMLConfig class and XML functions it uses. So I suppose we can do a test project that only requires TXMLConfig.

program testxmlconf;

uses XMLConf, sysutils;

var
  cfg: TXMLConfig;
  filename: string;

begin
  filename := ExtractFilePath(ParamStr(0))+'temp.xml';

  cfg := TXMLConfig.Create(nil);
  cfg.SaveToFile(filename);
  cfg.Free;

  cfg := TXMLConfig.Create(nil);
  cfg.LoadFromFile(filename);
  cfg.Free;
end.

It results indeed in the same memory leak:

Heap dump by heaptrc unit of /Users/johann/Documents/pascal/testxmlconf/testxmlconf
202 memory blocks allocated : 39538/40008
188 memory blocks freed     : 33542/34000
14 unfreed memory blocks : 5996
True heap size : 786432 (64 used in System startup)
True free heap : 777216
Should be : 777672
Call trace for block $0000000101846C00 size 38
Call trace for block $0000000101846000 size 32
Call trace for block $00000001017F5DB0 size 3080
  $000000010148F9FF
Call trace for block $0000000101846100 size 40
  $000000010148F9FF
Call trace for block $000000010183D600 size 84
Call trace for block $0000000101846400 size 32
Call trace for block $000000010183D740 size 98
Call trace for block $0000000101846300 size 32
Call trace for block $000000010183D880 size 96
Call trace for block $0000000101845E00 size 40
Call trace for block $00000001017F54D0 size 2048
Call trace for block $0000000101845F00 size 32
Call trace for block $000000010186D100 size 128
Call trace for block $0000000101865100 size 216
lainz commented 1 year ago

Ok. Do you want to report it? Or I do?

lainz commented 1 year ago

Reported: https://gitlab.com/freepascal.org/fpc/source/-/issues/40126

circular17 commented 1 year ago

Thank you very much

lainz commented 1 year ago

Seems that they fixed it in new record time!

rchastain commented 1 year ago

Good!