[SOLVED] FpSpreadSheet (WriteUTF8Text) memory leaks lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- JD · 2015-11-13 10:12 Hi there everyone, I have the following code segment that writes grid data to a spreadsheet. There are memory leaks on line 12 (with WriteUTF8Text) in the code below: 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 // create the workbookMyWorkbook := TsWorkbook.Create;//try  //  MyWorksheet := MyWorkbook.AddWorksheet('Rapport Ad-hoc');  // copy the grid contents  for intCountRow := 1 to kgrd.RowCount - 1 do        begin    for intCountCol := 1 to kgrd.ColCount - 1 do        begin      MyWorksheet.WriteUTF8Text(intCountRow, intCountCol - 1, kgrd.Cells[intCountCol, intCountRow]);    end;  end;   // write the workbook to file  try    MyWorkbook.WriteToFile(UTF8Decode(FileSaveAs1.Dialog.FileName), fmt, True);    //    MessageDlg(Format('Fichier %s crée avec succes', [FileSaveAs1.Dialog.FileName]), mtInformation, [mbOK], 0);  except    MessageDlg(Format('La création du fichier %s a echoué. Veuillez réessayer.', [FileSaveAs1.Dialog.FileName]), mtWarning, [mbOK], 0);  end;finally  // free the workbook  FreeAndNil(MyWorkbook);end;      // try...finally (MyWorkbook Create)  The memory leaks are in the attached screenshot. I did some digging & I found that the memory leak originates from line 397 of fpsclasses.pas 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 {@@ ------- ---------------------------------------------------------------------  Adds a new node to the tree identified by the specified row and column  indexes.------------------------------------------------------ -------------------------}function TsRowColAVLTree.Add(ARow, ACol: Cardinal): PsRowCol;begin  Result := NewData;                  <---------  Line 397 MEMORY LEAK HERE!  Result^.Row := ARow;  Result^.Col := ACol;  inherited Add(Result);end;  How can I get round this problem? Thanks a lot. JD ---------------------------------------------------------------------- wp · 2015-11-13 15:02 I don't see this memory leak here. For testing, I was using the excel8write demo (in folder examples\read_write\excel8demo) with the option heaptrc set in the project inspector. This demo creates row and column records and principally should be affected by this memory leak. Which fpspreadsheet version are you using? Is is probably too old. ---------------------------------------------------------------------- JD · 2015-11-13 15:48 I'm using version 1.6. You are right though because when I changed the line to MyWorksheet.WriteUTF8Text(intCountRow, intCountCol - 1, 'Sample'); the leaks disappeared! Replacing kgrd.Cells[intCountCol, intCountRow] with 'Sample' cleared the leaks. It's really curious because as long as I don't create the worksheet, everything is fine BUT once I write the grid contents to a worksheet, I get the memory leaks. The worksheet created is perfect nonetheless. JD ---------------------------------------------------------------------- rvk · 2015-11-13 15:55 Could you create a small test-project which stands on its own to test (which exposes this leak)? So it seems that accessing the cells in kgrd creates the leak. If you only loop from 1 to 1, how much is the leak then? Is it less than that 750 blocks? (How many cells in total does your original kgrd have?) ---------------------------------------------------------------------- wp · 2015-11-13 16:17 Is kgrd an ordinary TStringGrid or something else like a TKGrid? In case of a 3rd-party grid, do you see the same issue with a TStringGrid? ---------------------------------------------------------------------- JD · 2015-11-13 19:57 Eureka! I found it. It seems the problem arises when the cell in the grid is empty so I tested the contents of the grid & skipped all empty cells. Thanks for the tip rvk Now it is working perfectly & there are no more memory leaks. I also discovered the scenario below when WriteUTF8Text encounters empty or single space strings 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 MyWorksheet.WriteUTF8Text(intCountRow, intCountCol - 1, ' ');       // No memory leak MyWorksheet.WriteUTF8Text(intCountRow, intCountCol - 1, '');        // memory leak  Thanks to wp & rvk for your responses. It helped me focus my debugging. @wp It seems you've removed the save to HTML. I had to manually create an HTML file. Thanks for the wonderful component nonetheless. JD PS: kgrd is a TKGrid ---------------------------------------------------------------------- rvk · 2015-11-13 20:04 Oe... that one should be handled correctly by fpspreadsheet so if it's not I'm sure wp can build in a quick check for it in WriteUTF8Text itself (and empty the cell if you want to write '' to it). 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 MyWorksheet.WriteUTF8Text(intCountRow, intCountCol - 1, ' ');       // No memory leak MyWorksheet.WriteUTF8Text(intCountRow, intCountCol - 1, '');        // memory leak  Your check to only write if the cell is not empty works for you but fpspreadsheet should empty the cell if there is already content there and you want to write '' to it. ---------------------------------------------------------------------- wp · 2015-11-13 20:32 Hey, you guys are too fast for me... The new revision in svn fixes the memory leak created when passing an empty string to TWorksheet.WriteText (or: WriteUTF8Text - I am removing that strange UTF8 insert because strings in fpspreadsheet have been UTF8 for some time anyway). JD, saving to HTML is still there - I just tested it. Maybe you forgot to "use" the fpsHtml unit? ---------------------------------------------------------------------- JD · 2015-11-13 20:50 Quote from: wp on November 13, 2015, 09:32:00 pmHey, you guys are too fast for me... The new revision in svn fixes the memory leak created when passing an empty string to TWorksheet.WriteText (or: WriteUTF8Text - I am removing that strange UTF8 insert because strings in fpspreadsheet have been UTF8 for some time anyway). JD, saving to HTML is still there - I just tested it. Maybe you forgot to "use" the fpsHtml unit? I don't have fpsHTML.pas at all. I counted 30 files with fps*.pas search & fpsHTML.pas was not one of them. It is not in the files I unzipped from the 1.6 version. JD ---------------------------------------------------------------------- wp · 2015-11-13 21:02 Sorry I missed that you wrote that you use 1.6 - HTML is not included there. Also, you won't have the fix mentioned above. You'll have to upgrade to trunk (version 1.7). ---------------------------------------------------------------------- JD · 2015-11-13 22:25 Quote from: wp on November 13, 2015, 10:02:09 pmSorry I missed that you wrote that you use 1.6 - HTML is not included there. Also, you won't have the fix mentioned above. You'll have to upgrade to trunk (version 1.7). OK thanks a lot.