Drawgrid bug? lazarus / 31-LCL ---------------------------------------------------------------------- Zaxxon · 2017-01-11 09:50 I get a crash when I in the attached example, move the mouse pointer fairly fast, between the two red cells. (Win 7 64 bit Lazarus 1.6 FPC 3.0.0) In 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 function TCustomGrid.doRowSizing(X, Y: Integer): Boolean;var  Offset: Integer;begin  Result:=False;   with FSizing do   if gsRowSizing = fGridState then begin    if FUseXORFeatures then begin      if (y-OffIni)<=0 then        y:= OffIni;      if y<>PrevOffset then begin        if PrevLine then          DrawXorHorzLine(PrevOffset);      DrawXorHorzLine(Y);        PrevLine:=True;        PrevOffset:=y;    end;    end else      ResizeRow(Index, y-OffIni);    HeaderSizing(false, Index, y-OffIni);    Result:=True;  end else  if (fGridState=gsNormal) and (RowCount>FixedRows) and     ((FlipX(X)FGCache.FixedHeight) then  begin     // find closest cell and cell boundaries    if Y>FGCache.GridHeight-1 then      Index := RowCount-1    else      OffsetToColRow(False, True, Y, Index, OffEnd{dummy});    ColRowToOffset(False, True, Index, OffIni, OffEnd);     // find out what cell boundary is closer to Y    if OffEnd>FGCache.ClientHeight then      Offset := FGCache.ClientHeight    else    if (OffEnd-Y)<(Y-OffIni) then      Offset := OffEnd    else begin      Offset := OffIni;      Dec(Index);       if Index=-1 then begin             doRowSizing:=False;        Exit;      end;       ColRowToOffset(False, True, Index, OffIni, OffEnd);           end;   // check if it's not fixed row and if cursor is close enough to    // selected boundary    if (Index>=FFixedRows)and(Abs(Offset-Y)<=2) then begin      // start resizing      if Cursor<>crVSplit then begin      ChangeCursor(crVSplit);        PrevLine := False;        PrevOffset := -1;      end;      exit(true);    end   end;   if (cursor=crVSplit) then    ChangeCursor;end;  C:\Lazarus\lcl\Grids.pas I made a temporary fix by adding 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 Index=-1 then begin                doRowSizing:=False;        Exit;      end;   Can anyone confirm this bug? ---------------------------------------------------------------------- wp · 2017-01-11 10:09 Cannot confirm. Is the test meant to be done with mouse button pressed or not? What does your description ("move mouse between the two red cells") have to do with RowSizing? A comment on your sample: Only for changing the cell background color, the OnDrawCell event is overkill. Use the OnPreparecanvas instead and change the brush.color there: 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.DrawGrid3PrepareCanvas(sender: TObject; aCol, aRow: Integer;  aState: TGridDrawState);begin  if (aCol=3) and (ARow=17) then begin  DrawGrid3.Canvas.Brush.Color:=clRed;    DrawGrid3.Canvas.Brush.Style:=bsSolid;  end;end; ---------------------------------------------------------------------- Zaxxon · 2017-01-11 10:16 Just move mouse pointer back and forward (mouse button not pressed), between the red cells several times. goRowSizing is set to true in both Drawgrids. I get Index=-1 in doRowSizing after a while. And thats what causes the crash when ColRowToOffset is called. ---------------------------------------------------------------------- howardpc · 2017-01-11 11:06 I can confirm the behaviour on Linux (gtk2). I suggest patching grids.pas like this: 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 function TCustomGrid.ColRowToOffset(IsCol, Relative: Boolean; Index:Integer;  var StartPos, EndPos: Integer): Boolean;var  Dim: Integer;begin  Result:=false;  if (index < 0) then // <-- add this check for invalid low index value    Exit;             // a check for invalid high index values is already present  with FGCache do begin    if IsCol then begin      if index>ColCount-1 then        exit;      StartPos:=integer(PtrUInt(AccumWidth[index]));      Dim:=GetColWidths(index);    end else begin      if (index>RowCount-1) then        exit;      StartPos:=integer(PtrUInt(AccumHeight[index]));      Dim:= GetRowHeights(index);    end;                                  ... etc. ---------------------------------------------------------------------- wp · 2017-01-11 11:11 I still cannot reproduce. You write you have Win 64bit - is your Lazarus 64 bit as well, or do you use the 32 bit version (like myself)? I am not sure why it is harmful that this Index is -1. You write it causes as crash in ColRowToOffset. But this method returns at the very beginning if the index is -1. So, I am not sure if your analysis is correct. Another observation regarding the doRowSizing: After setting a breakpoint in doRowSizing, I do see indeed that this method is entered whenever the mouse is moved even if no button is pressed. This is strange: to me, Col/RowSizing means that a button must be pressed to drag the mouse, i.e. the MouseMove of the grid should take care of this 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 TCustomGrid.MouseMove(...);begin...  case FGridState of  ...  else    begin      if Shift = [ssLeft] then begin         // <--- new        if goColSizing in Options then          doColSizing(X,Y);         if goRowSizing in Options then          doRowSizing(X,Y);      end;                                  // <-- new      ...        With this modification the doRowSizing code would only be entered if the left mouse button is pressed and your problem should not occur any more. ---------------------------------------------------------------------- wp · 2017-01-11 11:14 @howardpc: I am using trunk, and (index < 0) is already checked (at slightly different locations). That's probably the reason why I don't see the bug. I do see it with Laz 1.6.2 now. @Zaxxon: Any chance that you can try trunk? ---------------------------------------------------------------------- balazsszekely · 2017-01-11 12:24 With Lazarus trunk I'm not able to reproduce the bug either. ---------------------------------------------------------------------- Thaddy · 2017-01-11 12:38 Quote from: GetMem on January 11, 2017, 01:24:30 pmWith Lazarus trunk I'm not able to reproduce the bug either. That's not really the point. The point is the code should be safe. It isn't. (I can reproduce this over multiple monitor setups) I suggest to include the above check by Howardpc or a similar one. I am using trunk (1.7) but an older one. ---------------------------------------------------------------------- balazsszekely · 2017-01-11 12:59 @Thaddy QuoteThat's not really the point. The point is the code should be safe. It isn't. (I can reproduce this over multiple monitor setups)That's exactly the point. The code is already safe in Lazarus trunk, I wasn't able to reproduce the bug. ---------------------------------------------------------------------- wp · 2017-01-11 18:02 Quote from: Thaddy on January 11, 2017, 01:38:13 pmI am using trunk (1.7) but an older one. Thaddy, your trunk is older than the release version... The check for "Index < 0"  has been added in r.51980 which was on March 17 last year. Could you imagine any reason why DoRowSizing (which calls the ColRowToOffset we are talking about) is called at all when the mouse is moved over the grid, no button pressed? (See my post above) If not then it should be avoided - unnecessary calls which are removed cannot cause trouble. [EDIT] It is for displaying the split cursor.