Recent

Author Topic: SIGSEGV in TStringGrid.Free when focused  (Read 16684 times)

mmxngg

  • New Member
  • *
  • Posts: 29
SIGSEGV in TStringGrid.Free when focused
« on: December 12, 2012, 11:32:03 pm »
Seems a bug but...http://bugs.freepascal.org/view.php?id=23478

So there is no way to create or manage a grid at runtime ? Really ?  :-[
« Last Edit: December 17, 2012, 06:07:10 pm by mmxngg »
Now i'm on Win7/32bits with Lazarus 1.0.4 FPC 2.6.0

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4715
  • I like bugs.
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #1 on: December 13, 2012, 12:08:59 am »
So there is no way to create or manage a grid at runtime ? Really ?  :-[

Yes there is but you freed a component that was added on a form at design time. It is a no no...

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

mmxngg

  • New Member
  • *
  • Posts: 29
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #2 on: December 13, 2012, 12:24:12 am »
Ah ok...but i posted a simple example for reproduce the case  ;)

This is a portion of real code in my project :

Code: [Select]
  SGDomanda := TStringGrid.Create(SBQuestionario);
  with (SGDomanda) do
  begin
    Left                     := $10;
    Top                      := CPos;
    Width                    := $28E;
    Height                   := $30;
    FixedCols                := $0;
    RowCount                 := $2;
    ScrollBars               := ssNone;
    TitleStyle               := tsNative;
    Options                  := [goEditing, goTabs];
    Parent                   := SBQuestionario;
    SGFixedCol               := Columns.Add;
    SGFixedCol.Width         := $1F8;
    SGFixedCol.Title.Caption := 'Domanda';
    SGFixedCol               := Columns.Add;
    SGFixedCol.Width         := $4C;
    SGFixedCol.Title.Caption := 'Risposte (da)';
    SGFixedCol               := Columns.Add;
    SGFixedCol.Width         := $46;
    SGFixedCol.Title.Caption := 'Risposte (a)';
    Name                     := 'SGDM' + IdxLastD;
    OnExit                   := @ResetGridPos;
    OnKeyDown                := @ChangeGridFocus;
    OnSelectCell             := @ScrollView;
  end;
  ....
  SGDomanda.SetFocus;
  ....
  SGDomanda.Free; // SIGSEGV

Of course without the focus all works fine.
« Last Edit: December 13, 2012, 12:29:22 am by mmxngg »
Now i'm on Win7/32bits with Lazarus 1.0.4 FPC 2.6.0

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4715
  • I like bugs.
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #3 on: December 13, 2012, 12:45:39 am »
You still don't need to free it because the Owner is SBQuestionario (whatever it is).
If you really want to free it explicitly, do this :
Code: [Select]
SGDomanda := TStringGrid.Create(Nil);

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

mmxngg

  • New Member
  • *
  • Posts: 29
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #4 on: December 13, 2012, 01:11:14 am »
SBQuestionario is a ScrollBox added on a form at design time and can hold one or more grid created at runtime by the user.
SGDomanda is a simply dummy variable for create a new grid but i have to manage all by component name because the user can add or remove one or more grid. I have the same management for all components generated at runtime and only the grid has this problem.
Now i'm on Win7/32bits with Lazarus 1.0.4 FPC 2.6.0

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #5 on: December 13, 2012, 01:43:27 am »
as far as I can understand calling setfocus and then free creates some kind of conflict in the components message handling loop I assume that the setfocus use some kind of async method to handle the shift of focus state which is executed after the component's free is called and of course it raises the sigsegv error.

try to avoid calling it there is no reason to call focus, I do not have all the code to see why you need it but since you are creating and freeing the component in the same routine I see no reason to change the focus state at all. by the way I really do not see a reason to use a stringgrid at all if it is created and released inside a single routine try to use a non visual class instead.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mmxngg

  • New Member
  • *
  • Posts: 29
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #6 on: December 13, 2012, 02:19:31 am »
The focus is set on the creation routine but of course the free is called elsewhere.

This is a screenshot of the application with the ScrollBox that contains multiple grids that may be deleted, modified or added through the buttons on the right and bottom (also the buttons inside the ScrollBox are created at runtime).

http://postimage.org/image/mk7wm1wfp/

I can not post 3512 lines of code to explain that there is an error in freeing a grid that has the focus. For me it is an obvious bug, beyond how they are created, managed or released. To work around this problem i will check the component type before releasing it and remove the focus if necessary...
Now i'm on Win7/32bits with Lazarus 1.0.4 FPC 2.6.0

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #7 on: December 13, 2012, 03:32:02 am »
I'm sorry, but the test code you have provided was simplified to much, making it impossible to get the correct flow of the problem. Any way you are right this is a bug and needs to be addressed.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mmxngg

  • New Member
  • *
  • Posts: 29
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #8 on: December 13, 2012, 12:25:04 pm »
A simple and complete example to understand the case. I used the SpeedButton that do not change the focus.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Grids, Buttons;

type

  { TForm1 }

  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ScrollBox1: TScrollBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
  private
    { private declarations }
    NGrid: Integer;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  NGrid := $0;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  Grid: TStringGrid;
begin
  Grid := TStringGrid.Create(ScrollBox1);
  with (Grid) do
  begin
    Top       := Random(Form1.Height);
    Left      := Random(Form1.Width);
    Height    := 48;
    Width     := 132;
    FixedCols := 0;
    FixedRows := 0;
    RowCount  := 2;
    ColCount  := 2;
    Parent    := ScrollBox1;
    Name      := 'Grid' + Format('%.4d', [NGrid]);
    ComboBox1.Items.Add(Name);
  end;
  Inc(NGrid);
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  if not (ComboBox1.ItemIndex < 0) then
  begin
    ScrollBox1.FindComponent(ComboBox1.Caption).Free;
    ComboBox1.Items.Delete(ComboBox1.ItemIndex);
    ComboBox1.Refresh;
  end;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
  if not (ComboBox1.ItemIndex < 0) then
    TStringGrid(ScrollBox1.FindComponent(ComboBox1.Caption)).SetFocus;
end;

end.
Now i'm on Win7/32bits with Lazarus 1.0.4 FPC 2.6.0

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #9 on: December 13, 2012, 12:50:31 pm »
Problem is in use of method Free. Use FreeThenNil instead. It is in unit LCLProc.
Code: [Select]
procedure TForm1.SpeedButton2Click(Sender: TObject);
var
  Grid: TStringGrid;     
begin
  if not (ComboBox1.ItemIndex < 0) then
  begin
    Grid:=TStringGrid(ScrollBox1.FindComponent(ComboBox1.Items[ComboBox1.ItemIndex]));
    FreeThenNil(Grid);
    ComboBox1.Items.Delete(ComboBox1.ItemIndex);
    ComboBox1.Refresh;
  end;
end;       

also, isn't it more handy to write
Code: [Select]
if ComboBox1.ItemIndex >= 0 then ...:)
« Last Edit: December 13, 2012, 01:42:01 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mmxngg

  • New Member
  • *
  • Posts: 29
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #10 on: December 13, 2012, 01:46:19 pm »
I used PerformTab to move the focus to the next component (most useful in my case). Both methods work but it is a trick to bypass the problem... no ?

also, isn't it more handy to write
Code: [Select]
if ComboBox1.ItemIndex >= 0 then ...:)
It 's the same...school of thought  :) (maybe the compiler changes the jump instruction but all have the same cycle)
Now i'm on Win7/32bits with Lazarus 1.0.4 FPC 2.6.0

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #11 on: December 13, 2012, 02:24:53 pm »
I am not sure but IMO it will be related to focus. You use TSpeedButton to delete selected Grid but TSpeedButton itself cannot have focus (it is derived from TGraphicControl). Therefore Lazarus tries to keep focus on selected component which deleted at the same time - > crash  :).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #12 on: December 13, 2012, 05:31:16 pm »
@Blaazen

Actually inability to receive focus means that there is no focus shift by the mouse and no kind of effort to keep the focus on the already focused component.

Have you actually looked at the call stack when the exception is raised? Expand it to show more that 10 previous results and you will see that the exception is raised when the setfocus is called to shift the focus to the form it self.

I'm assuming that the problem is that the widgetset.Setfocus  is called twice once from the form and once from the component receiving the focus, that needs to be cleaned up I guess but I'm not well versed in the lcl to actually have an opinion at this point..

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #13 on: December 13, 2012, 05:55:52 pm »
Here it shows:
Code: [Select]
#0 CLASSES$_$TLIST_$__$$_GET$LONGINT$$POINTER at :0
#1 ?? at :0
#2 COLROWTOOFFSET(0x7ffff7fabf30, true, true, 0, 1431655765, 1431655765) at grids.pas:5506
#3 CELLRECT(0x7ffff7fabf30, 0, 0) at grids.pas:3137
#4 INVALIDATECELL(0x7ffff7fabf30, 0, 0, false) at grids.pas:7251
#5 INVALIDATECELL(0x7ffff7fabf30, 0, 0) at grids.pas:7239
#6 INVALIDATEFOCUSED(0x7ffff7fabf30) at grids.pas:6802
#7 DOEXIT(0x7ffff7fabf30) at grids.pas:6480
#8 CMEXIT(0x7ffff7fabf30, {MSG = 45083, UNUSEDMSG = 1431655765, WPARAM = 0, LPARAM = 0, RESULT = 0}) at include/wincontrol.inc:8146
#9 SYSTEM$_$TOBJECT_$__$$_DISPATCH$formal at :0
#10 ?? at :0
#11 ?? at :0
#12 ?? at :0
#13 ?? at :0
#14 ?? at :0
#15 ?? at :0
#16 ?? at :0
#17 .Ld178 at :0
#18 ?? at :0
#19 ?? at :0
Probably it may differ on other platform (I have Qt4).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: SIGSEGV in TStringGrid.Free when focused
« Reply #14 on: December 13, 2012, 07:49:02 pm »
It 's the same...school of thought  :) (maybe the compiler changes the jump instruction but all have the same cycle)
It actually isn't. More obfuscated, less easy to understand.
But of course if you're the only one maintaining it, no problem.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

 

TinyPortal © 2005-2018