Author Topic: [SOLVED]problems about memory leak  (Read 623 times)


  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
[SOLVED]problems about memory leak
« on: November 23, 2022, 05:56:14 am »

I found when using TsWorkbookSource.LoadFromFile or TsWorkbookSource.WorkBook.LoadFromStream, with file format sfOpenDocument or sfOOXml, the application will raise exception of memory leak when it terminated.
Further more, diffrent file format makes different count of memory leak blocks, but diffrent method, LoadFromFile or LoadFromStream, with same file format makes same count of memory leak blocks.
my code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.loadSpreadSheet;
  2. var
  3.  rs: TResourceStream;
  4. begin
  5.   rs := TResourceStream.Create(HINSTANCE, 'TEST_FILE', 'FILE');
  6.   try
  7.     // ws is TsWorkbookSource putted on the form
  8.     ws.Workbook.ReadFromStream(rs, sfOpenDocument); // no this line, no memory leak
  9.   finally
  10.     rs.Free;
  11.   end;
  12. end;
My Enviroment:
  OS: Win10 x64
  Lazarus: 2.0.10 x64
  FPC: 3.2.0 x64
  SVN: 63526
  FPSpreadsheet: installed from "Online Package Manager"

The attachment is the spreadsheet file of OpenDocument format produced by LibreOffice
I'm not sure it is a bug or something I did wrong?

Best regards.
« Last Edit: December 10, 2022, 03:23:18 am by kinlion »


  • Hero Member
  • *****
  • Posts: 10862
Re: problems about memory leak
« Reply #1 on: November 25, 2022, 12:15:59 pm »
Code: Pascal  [Select][+][-]
  1.   rs := TResourceStream.Create(HINSTANCE, 'TEST_FILE', 'FILE');
AFAIK, there is no resource type "FILE", I've only seen the RT_XXXX constants as last argument here. And for a file embedded in a resource, I'd expect it to be RT_RCDATA (in unit LCLType). So, the correct call, in my opinion, would be

Code: Pascal  [Select][+][-]
  1.   rs := TResourceStream.Create(HINSTANCE, 'TEST_FILE', RT_RCDATA);

And when I embed a file created by myself (using LibreOffice v7.3.7) into the the resources (Project options > Resources > Add), the file can be read correctly without an error, and without leaving a memory leak.

With your file, however, I am getting an arithmetic overflow, and even my LibreOffice Calc complains about too many columns when I try to load it.

According to //, an ods file up to v7.3 can hold at most 1024 columns. And in fact, the "table:table-cell" node of each row in your ods file looks like this:
Code: XML  [Select][+][-]
  1. <table:table-cell table:number-columns-repeated="16372" />
This means that there are at least 16372 columns - way too much. I noticed in above specification link, however, that Calc v7.4 allows up to 16384 columns, but FPSpreadsheet is not yet up-to-date with that new specification.

The ods reader of FPSpreadsheet does detect this error situation and refuses to read cells if they were found beyond the 1024 columns limit. But in this case the method TsSpreadOpenDocReader.ReadCell did return an undefined value in the argument AColsRepeated.

This is fixed now in the svn repository. You can fix your OPM version easily, too: Open file fpsOpenDocument.pas, find the code for this method, and add "AColsRepeated := 0;" as first statement. Then recompile the fpspreadsheet package.

After this modification I could also load your file without error, and without memory leak.


  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Re: problems about memory leak
« Reply #2 on: December 10, 2022, 03:21:48 am »
Thanks a lot to wp.  :)


  • Jr. Member
  • **
  • Posts: 82
  • I Love Lazarus
Re: problems about memory leak
« Reply #3 on: December 10, 2022, 03:44:00 am »
AFAIK, there is no resource type "FILE"

I'm using resource by add a .rc file to my project, in a .rc file, one can define any resource type instead of system predefined type consts.

for example, add a line in project file:
Code: Pascal  [Select][+][-]
  1. {$R test.rc}

and write a single line in test.rc file as:

TEST_JPG                IMAGE_FILE         'test.jpg'

in a .rc file, each line describe a resource,
and a line is seperated into 3 parts by blank char(s), both spaces or tabs are OK.
1st part: TEST_JPG is the resource name
2nd part: IMAGE_FILE is the resource type
3rd part: test.jpg is the file of the resource data

if the resource type does not match any system predefined SYMBOL, then it becomes
the customized resource type named by the string, IMAGE_FILE in the above example.

if you want the resource type to be RT_RCDATA, just replace the IMAGE_FILE by RCDATA.

The .rc file format is explained by Microsoft, I don't know if it is OK on other OS


TinyPortal © 2005-2018