Recent

Author Topic: Can more people test this code, Maybe Bug in Lazarus-Svn-Version.?  (Read 7060 times)

SunyD

  • Guest
EDIT:
also look at latest entry:
http://forum.lazarus.freepascal.org/index.php/topic,26912.msg166042.html#msg166042
----------
Hi,
I create 3 components in TForms OnCreate-Event and desroy it in OnDestroy-Event.
2 components (TStringGrid, TDataModule) cause error and 1 component (TRadioGroup) destroyed without error.
I think the error happens when LCL/FCL tries to free component.
I know I can remove the codes in OnDestroy and let them automatic destroyed.

Is it my mistake how to use components or is it bug?

Code: [Select]
  TForm1 = class(TForm)
//...
  public
    { public declarations }
    MyEdit: TEdit;
    MyGrid: TStringGrid;
    MyRadio: TRadioGroup;
    myModule: TDataModule1;
  end;

//....

implementation

//...

procedure TForm1.FormCreate(Sender: TObject);
begin
  // I create here 4 Components. TStringGrid and TDataModule cause error on FormDestroy.
  // Why only 2 and not all 3 ?
  // I create and destroy all similar
  MyRadio:= TRadioGroup.Create(self);
   MyRadio.Align:=alLeft;
   MyRadio.Items.Text:='Radio1'+LineEnding+'Radio2'+LineEnding+'Radio3';
   MyRadio.Parent:=self;

  MyEdit:= TEdit.Create(self);
   MyEdit.Align:=alBottom;
   MyEdit.Parent:=self;

  MyGrid:= TStringGrid.Create(self);
   MyGrid.Align:=alClient;
   MyGrid.Parent:=self;

  // I removed DataModule1 from autocreate-form-list in projectoptions
  //so i create manually here
  myModule:= TDataModule1.Create(self);

  ActiveControl:=MyRadio;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //Next Line is O.K.
  if Assigned(MyEdit) then FreeAndNil(MyEdit);
  //Next Line is O.K.
  if Assigned(MyRadio) then FreeAndNil(MyRadio);

  //Next line causes "External:SIGSEGV" error, IF IT HAS FOCUS on close form
  //and started from Lazarus-IDE
  if Assigned(MyGrid) then FreeAndNil(MyGrid);

  //Next line causes "External:SIGSEGV" error, everytime if it excecuted
  //not in next line, the error comes in other function (maybe TForm.Destroy?)
  if Assigned(myModule) and (chbxFreeDmOnDestroy.Checked) then FreeAndNil(myModule);
end;


EDIT:
I forget to say. I if create a Button and destroy all components from above in Buttons OnClick-Event, then the code in TForm1.OnDestroy don't cause any error.
« Last Edit: December 29, 2014, 04:24:02 am by Soner A. »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Create/Destroy components at Runtime. Is it my mistake or bug?
« Reply #1 on: December 29, 2014, 12:20:21 am »
What OS and widgetset do you use?
Your program causes no errors here (win32).

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Create/Destroy components at Runtime. Is it my mistake or bug?
« Reply #2 on: December 29, 2014, 01:16:23 am »
Soner, it is your mistake.

SunyD

  • Guest
Re: Create/Destroy components at Runtime. Is it my mistake or bug?
« Reply #3 on: December 29, 2014, 01:33:33 am »
Soner, it is your mistake.
Engkin, why TEdit,  TRadioGroup and maybe some other controls don't cause error but TStringGrid and TDataModule cause error?
And why it doesn't cause errors when I free the controls before TForm.OnDestroy called?

What OS and widgetset do you use?
Your program causes no errors here (win32).
Lazarus 1.3 Svn-Rev. 46679, fpc 2.6.4, WinXp, All 32-Bit.

Maybe my lazarus has error. have you did this?
You get error if you run the programm within Lazarus-IDE.
1) When you select StringGrid (any Cell is marked) and close the programm you get error
2) or when you check the checkbox and close the programme then you get error.

SunyD

  • Guest
Re: [SOLVED] Create/Destroy components at Runtime. Is it my mistake or bug?
« Reply #4 on: December 29, 2014, 02:48:25 am »
Hi,
Now, I installed in Win7 official lazarus-1.2.6-fpc-2.6.4-win32 and run the example inside in the IDE without Errors  :)
Then I rebuild this Lazarus with AnchorDock-Package and run the example again in the IDE with the same errors! (Example Projects doesn't use AnchorDock, Only Lazarus) %)
Then I uninstalled AnchorDock-Package rebuild lazarus and run the example again in the IDE without errors  :)

Later i tested it on my first Lazarus-installation(look my signatur). First i removed AnchorDock and rebuild Lazarus bu the errors is still there.  %)

Conclusion: Maybe AnchorDock-package causes Error or LCL has new functions since last release that doesn't work good with AnchorDock-package.

Can anyone test this example with and witggout AnchorDock-installed in the IDE?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] Create/Destroy components at Runtime. Is it my mistake or bug?
« Reply #5 on: December 29, 2014, 03:00:21 am »
Do you get these errors if your create the four controls with Owner set to nil?

SunyD

  • Guest
Re: [SOLVED] Create/Destroy components at Runtime. Is it my mistake or bug?
« Reply #6 on: December 29, 2014, 03:43:45 am »
Do you get these errors if your create the four controls with Owner set to nil?
Yes, but that is not the problem and not all four only TStringGrid and TDataModule cause error. I think the problem is located in "how lcl-destroy its child components".
Maybe my Lazarus version has error. I"m on the internet with mobile connection now when I connected with wifi then i download latest trunk-version and test it again.

A minute ago i tested official 1.2.6 version on WinXp-machine and it run's without errors, with and without anchordock.  %)

I  think don't make mistake with manually destroying. I make it since over 16 years, if i create them manually. It's not needed but it's better to avoid memory holes.

I am tired now, i inspect it another day.
Maybe some people download example-code and run it inside ide and report here.

Thanks for your replies.

SunyD

  • Guest
Can more people test this code, Maybe Bug in Lazarus-Svn-Version?
« Reply #7 on: December 29, 2014, 04:22:07 am »
Can more people test this code, Maybe Bug in Lazarus-Svn-Version.?

I can't give it up :)

So I tested again, here ist results:
1)
Lazarus official release 1.2.6 fpc 2.6.4 on Win7:
-without addional components installed: no Errors
-with AnchorDock installed: no Errors

same Lazarus on WinXP:
-without addional components installed: no Errors
-with AnchorDock installed: no Errors
2)
Lazarus 1.3 Svn-Rev.46679 (not latest), fpc 2.6.4 on WinXP:
-with many 3rd part components installed: Error
-with many 3rd part components installed but without AnchorDock: Error
Conclusion: AnchorDock doesn't cause it.

How to test?
-Download the test project and run it inside Lazarus-IDE.
- 1. click on the Stringgrid on the right and close the programm.
- 2. check the checkbox on the top-left and close the programm.

If you get errors report it here with your lazarus and os version.

NOTE: you get only error it you run INSIDE the IDE. [Menu-Start-Run]
« Last Edit: December 29, 2014, 04:26:03 am by Soner A. »

kapibara

  • Hero Member
  • *****
  • Posts: 656
Re: Can more people test this code, Maybe Bug in Lazarus-Svn-Version.?
« Reply #8 on: December 29, 2014, 08:24:13 am »
With Lazarus Win32 Release version: no problem.

But with Lazarus 64bit development version I get a SIGSEG. Even though it is compiled with, and uses, 64bit FPC 2.71 which supports SEH.

The error goes away if you set MyGrid.Parent:=nil before freeing MyGrid:

Code: [Select]
  if Assigned(MyGrid) then
  begin
    MyGrid.Parent:=nil;
    FreeAndNil(MyGrid);
  end;

But I can't see anything wrong with your code. Objects are tested for nil before they are destroyed:

Code: [Select]
if Assigned(MyGrid) then FreeAndNil(MyGrid);
The owner shouldnt try to free this nil object. A thought: could it be that when myGrid is free'd, it is not removed from the owners list of owned objects? Possible? But why should that differ between 32 and 64 bit versions. And it doesnt help to create myGrid with nil as owner..

Then a discussion on StackOverflow gave a hint:

"Also worth noting is that if you destroy a parent, the children will get destroyed, too, even if they're not owned by the parent. –  Rob Kennedy Jan 26 '09 at 6:52"

And that led me to try and see what happens when Parent is set to nil. Bingo. But why it happens..

http://stackoverflow.com/questions/474448/why-do-some-delphi-components-require-aowner-tcomponent-to-construct-them


Also, I dont know if this could have an effect:

"Current released versions of FPC (2.6.x) do not include support for SEH handling. This can affect the stability of many 64 bit applications. Therefore it is recommended to use the 32bit IDE, and generate 32bit applications only. This will change when FPC 2.8.0 is released. Please visit our forums and Wiki for additional information. http://wiki.lazarus.freepascal.org/Windows_Programming_Tips#FPC_2.6.x.2FLazarus_warning_.28Missing_support_for_SEH.29"
« Last Edit: December 29, 2014, 09:36:42 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

SunyD

  • Guest
Re: Can more people test this code, Maybe Bug in Lazarus-Svn-Version.?
« Reply #9 on: December 29, 2014, 11:06:04 pm »
kapibara these errors don't occur in Delphi.
I downloaded today latest trunk (47264), TDataModule don't cause error more but TStringGrid-error still present.
« Last Edit: December 29, 2014, 11:07:40 pm by Soner A. »

kapibara

  • Hero Member
  • *****
  • Posts: 656
Re: Can more people test this code, Maybe Bug in Lazarus-Svn-Version.?
« Reply #10 on: December 29, 2014, 11:52:59 pm »
> kapibara these errors don't occur in Delphi.

They dont occur in Lazarus either with the released version. But sure, the trunk snapshot probably needs something. I have compiled the latest Lazarus trunk with both fpc 2.6.4 and 2.71 and it doesn't matter, same thing happens.
Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

BSaidus

  • Hero Member
  • *****
  • Posts: 647
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Can more people test this code, Maybe Bug in Lazarus-Svn-Version.?
« Reply #11 on: December 30, 2014, 02:20:24 pm »
Hi !
I think you should not free objects or components created this (Self) parameter in constructor since they automatically destroyed with the main container is distroyed
i.e:
Code: [Select]
  TForm1 = class(TForm)
//...
  public
    { public declarations }
    MyEdit: TEdit;
    MyGrid: TStringGrid;
    MyRadio: TRadioGroup;
    myModule: TDataModule1;
  end;

//....

implementation

//...

procedure TForm1.FormCreate(Sender: TObject);
begin
  // I create here 4 Components. TStringGrid and TDataModule cause error on FormDestroy.
  // Why only 2 and not all 3 ?
  // I create and destroy all similar
  MyRadio:= TRadioGroup.Create(self); // here the MyRadio will be destroyed when the Form1 is distroyed
                                                              // same thing for others.
   MyRadio.Align:=alLeft;
   MyRadio.Items.Text:='Radio1'+LineEnding+'Radio2'+LineEnding+'Radio3';
   MyRadio.Parent:=self;

  MyEdit:= TEdit.Create(self);
   MyEdit.Align:=alBottom;
   MyEdit.Parent:=self;

  MyGrid:= TStringGrid.Create(self);
   MyGrid.Align:=alClient;
   MyGrid.Parent:=self;

  // I removed DataModule1 from autocreate-form-list in projectoptions
  //so i create manually here
  myModule:= TDataModule1.Create(self);

  ActiveControl:=MyRadio;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //Next Line is O.K.
  if Assigned(MyEdit) then FreeAndNil(MyEdit);
  //Next Line is O.K.
  if Assigned(MyRadio) then FreeAndNil(MyRadio);

  //Next line causes "External:SIGSEGV" error, IF IT HAS FOCUS on close form
  //and started from Lazarus-IDE
  if Assigned(MyGrid) then FreeAndNil(MyGrid);

  //Next line causes "External:SIGSEGV" error, everytime if it excecuted
  //not in next line, the error comes in other function (maybe TForm.Destroy?)
  if Assigned(myModule) and (chbxFreeDmOnDestroy.Checked) then FreeAndNil(myModule);
end;

lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

 

TinyPortal © 2005-2018