Peculiar case of auto resize height in TStringGrid lazarus / 31-LCL ---------------------------------------------------------------------- kqha · 2022-04-04 15:07 This has been a real pain in the a** for me to narrow it down to this specific reproducible case of a auto resize height in TStringgrid. I need to resize my StringGrids to be able to auto height resize according to the contents, but there are times when my window freeze without proper error, and it happens randomly. I finally narrow it down to find that the freeze is caused by some kind of OnDrawCell endlessly called caused by the script. The peculiar thing is, this such case only happen when certain data loaded into the grid. To reproduce it, try: Make a new project with form not larger than 710x450Add a TStringGrid with size exactly 608x328Set the grid with Font size: 10Font name: Open Sans (probably works with other font, I just leave it at default and it work as well)Default drawing: falseFixed cols: 0Add 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 procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);var  bRect : TRect;  sTmp  : String;begin   with (Sender as TStringGrid) do begin    sTmp := Cells[aCol,Arow];    Canvas.FillRect(aRect);    Canvas.FillRect(bRect);    bRect := aRect;    DrawText(Canvas.Handle, PChar(sTmp), length(sTmp), bRect,             DT_CALCRECT or DT_WORDBREAK);    if (bRect.Bottom - bRect.Top) > DefaultRowHeight then     RowHeights[aRow] := bRect.Bottom - bRect.Top;    DrawText(Canvas.Handle, PChar(sTmp), length(sTmp), aRect, DT_WORDBREAK);   end;end;   procedure TForm1.FormCreate(Sender: TObject);begin  StringGrid1.RowCount := 10;  StringGrid1.Cells[0,0] := '#';  StringGrid1.Cells[1,0] := 'Title 1';  StringGrid1.Cells[2,0] := 'Title 2';  StringGrid1.Cells[3,0] := 'Title 3';  StringGrid1.Cells[0,1] := '1';  StringGrid1.Cells[1,1] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[2,1] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,1] := 'Num';  StringGrid1.Cells[0,2] := '';  StringGrid1.Cells[1,2] := '';  StringGrid1.Cells[2,2] := 'Not a very long text';  StringGrid1.Cells[3,2] := 'Num';  StringGrid1.Cells[0,3] := '2';  StringGrid1.Cells[1,3] := 'Not a very long text';  StringGrid1.Cells[2,3] := 'Not a very long text but should break to 3 lines';  StringGrid1.Cells[3,3] := 'Num';  StringGrid1.Cells[0,4] := '';  StringGrid1.Cells[1,4] := '';  StringGrid1.Cells[2,4] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,4] := 'Num';  StringGrid1.Cells[0,5] := '';  StringGrid1.Cells[1,5] := '';  StringGrid1.Cells[2,5] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,5] := 'Num';  StringGrid1.Cells[0,6] := '';  StringGrid1.Cells[1,6] := '';  StringGrid1.Cells[2,6] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,6] := 'Num';  StringGrid1.Cells[0,7] := '3';  StringGrid1.Cells[1,7] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[2,7] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,7] := 'Num';  StringGrid1.Cells[0,8] := '';  StringGrid1.Cells[1,8] := '';  StringGrid1.Cells[2,8] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,8] := 'Num';  StringGrid1.Cells[0,9] := '';  StringGrid1.Cells[1,9] := '';  StringGrid1.Cells[2,9] := 'Lorem Ipsum is simply dummy text of the printing and typesetting industry';  StringGrid1.Cells[3,9] := 'Num';end;  After I ran the code, the window freeze with StringGrid1 vertical scrollbar flickering endlessly, the only way to shut it down is by killing the app. Notice that if you alter the line 37 of the code above (one that set 'Not a very long text but should break to 3 lines') to either shorter or longer, the freeze didn't happen.  This happen with my Lazarus 2.0.6, upgraded it to 2.2.0 (FPC 3.2.2) today hoping that this is somehow a bug in Lazarus/FPC, yet ended the same. ---------------------------------------------------------------------- wp · 2022-04-04 15:14 Don't set RowHeights[row] in the OnDrawCell handler because changing the row heights will trigger another painting cycle leading to another OnDrawCell etc... ---------------------------------------------------------------------- kqha · 2022-04-04 15:26 Quote from: wp on April 04, 2022, 05:14:57 pmDon't set RowHeights[row] in the OnDrawCell handler because changing the row heights will trigger another painting cycle leading to another OnDrawCell etc... Based on your input, I changed the OnDrawCell slightly from 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 if (bRect.Bottom - bRect.Top) > DefaultRowHeight then     RowHeights[aRow] := bRect.Bottom - bRect.Top;to 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 if (bRect.Bottom - bRect.Top) > RowHeights[aRow] then     RowHeights[aRow] := bRect.Bottom - bRect.Top; This solves it!  Thank you, will try it on the real app source code and try it with other different data ---------------------------------------------------------------------- wp · 2022-04-04 15:43 Very risky, you are lucky that this does not hang... It definitely is not a stable situation. The rule is: in the OnPaint, OnDraw or whatever execute only drawing commands, nothing else. You must set the rowheights somewhere else. ---------------------------------------------------------------------- kqha · 2022-04-05 01:33 Quote from: wp on April 04, 2022, 05:43:45 pmVery risky, you are lucky that this does not hang... It definitely is not a stable situation. The rule is: in the OnPaint, OnDraw or whatever execute only drawing commands, nothing else. You must set the rowheights somewhere else. You're right, thinking again while it works for now, in the future there will be chance another peculiar thing will arise. Will look for another way to achieve my goal. Thank you very much