Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-10

Exception when I change betwen forms

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Usuario Anónimo

Sometimes in a simple applicattion whith a principal form and several secondary forms accesed by a TMainMenu I get the error:

Access violation.

Press Ok to ignore and risk data corruption.
Press Cancel to kill the program.

when I branch between forms. Anybody help me?

My application:

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus;
type
  { TForm1 }
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
var
  Form1: TForm1;
implementation
uses Unit2, Unit3, Unit4;

{ TForm1 }

procedure TForm1.MenuItem1Click(Sender: TObject);
begin
 Form2 := TForm2.Create (Application);
 try
  Form2.ShowModal;
 finally
  Form2.Free;
 end;
end;
procedure TForm1.MenuItem2Click(Sender: TObject);
begin
 Form3 := TForm3.Create (Application);
 try
  Form3.ShowModal;
 finally
  Form3.Free;
 end;
end;
procedure TForm1.MenuItem3Click(Sender: TObject);
begin
 Form4 := TForm4.Create (Application);
 try
  Form4.ShowModal;
 finally
  Form4.Free;
 end;
end;
initialization
  {$I unit1.lrs}
end.

Unit2.pas

unit Unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus;
type
  { TForm2 }
  TForm2 = class(TForm)
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    procedure MenuItem1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
var
  Form2: TForm2;
implementation

{ TForm2 }

procedure TForm2.MenuItem1Click(Sender: TObject);
begin
  Close;
end;
initialization
  {$I unit2.lrs}
end.

Regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081001/ee7a07bd/attachment-0001.html

Reply by email

Reenen Laurie
to Usuario Anónimo

You may need to post your application's .lpr as well... We don't know if the forms are created in there or not.

I am not sure you can assign "Appliction" as the parent of a form... isn't it just "null" or "self"? Smarter people will know that...

ie.
Form2 := TForm2.Create (self);
or
Form2 := TForm2.Create (null);
In the .lpr file the command is different too...
Application.CreateForm (TForm2, Form2);

Regards,
-Reenen

2008/10/1 Usuario An?nimo

Show quoted text 136 lines
Sometimes in a simple applicattion whith a principal form and several secondary forms accesed by a TMainMenu I get the error: Access violation. Press Ok to ignore and risk data corruption. Press Cancel to kill the program. when I branch between forms. Anybody help me? My application: unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus; type { TForm1 } TForm1 = class(TForm) MainMenu1: TMainMenu; MenuItem1: TMenuItem; MenuItem2: TMenuItem; MenuItem3: TMenuItem; MenuItem4: TMenuItem; procedure MenuItem1Click(Sender: TObject); procedure MenuItem2Click(Sender: TObject); procedure MenuItem3Click(Sender: TObject); private { private declarations } public { public declarations } end; var Form1: TForm1; implementation uses Unit2, Unit3, Unit4; { TForm1 } procedure TForm1.MenuItem1Click(Sender: TObject); begin Form2 := TForm2.Create (Application); try Form2.ShowModal; finally Form2.Free; end; end; procedure TForm1.MenuItem2Click(Sender: TObject); begin Form3 := TForm3.Create (Application); try Form3.ShowModal; finally Form3.Free; end; end; procedure TForm1.MenuItem3Click(Sender: TObject); begin Form4 := TForm4.Create (Application); try Form4.ShowModal; finally Form4.Free; end; end; initialization {$I unit1.lrs} end. Unit2.pas unit Unit2; {$mode objfpc}{$H+} interface uses Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus; type { TForm2 } TForm2 = class(TForm) MainMenu1: TMainMenu; MenuItem1: TMenuItem; procedure MenuItem1Click(Sender: TObject); private { private declarations } public { public declarations } end; var Form2: TForm2; implementation { TForm2 } procedure TForm2.MenuItem1Click(Sender: TObject); begin Close; end; initialization {$I unit2.lrs} end. Regards. _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus
o__ ,_.>/ _ (_)_\(_)_______ ...speed is good _______________ I believe five out of four people have a problem with fractions. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081002/81568cbf/attachment.html

Reply by email

Mattias Gärtner
to Reenen Laurie

Zitat von Reenen Laurie :

Show quoted text 5 lines
You may need to post your application's .lpr as well... We don't know if the forms are created in there or not. I am not sure you can assign "Appliction" as the parent of a form... isn't it just "null" or "self"? Smarter people will know that...

You confuse Parent and Owner.

TForm2.Create(Application) sets the Owner, not the Parent. Using Application as Owner for forms is normal and allowed. As Parent it won't even compile, because TApplication is not a TWinControl.
Owner = responsible for freeing component (can only be set at creation) Parent = visual parent (can be changed at any time)

To debug an access violation, run the program in the debugger and create a backtrace. The wiki explains how.

Mattias

Show quoted text 160 lines
ie. Form2 := TForm2.Create (self); or Form2 := TForm2.Create (null); In the .lpr file the command is different too... Application.CreateForm (TForm2, Form2); Regards, -Reenen 2008/10/1 Usuario An?nimo > Sometimes in a simple applicattion whith a principal form and several > secondary forms accesed by a TMainMenu I get the error: > > Access violation. > > Press Ok to ignore and risk data corruption. > Press Cancel to kill the program. > > when I branch between forms. Anybody help me? > > My application: > > unit Unit1; > > {$mode objfpc}{$H+} > > interface > > uses > Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus; > > > type > > { TForm1 } > > TForm1 = class(TForm) > MainMenu1: TMainMenu; > MenuItem1: TMenuItem; > MenuItem2: TMenuItem; > MenuItem3: TMenuItem; > MenuItem4: TMenuItem; > procedure MenuItem1Click(Sender: TObject); > procedure MenuItem2Click(Sender: TObject); > procedure MenuItem3Click(Sender: TObject); > private > { private declarations } > public > { public declarations } > end; > > var > Form1: TForm1; > > implementation > > uses Unit2, Unit3, Unit4; > > { TForm1 } > > procedure TForm1.MenuItem1Click(Sender: TObject); > begin > Form2 := TForm2.Create (Application); > try > Form2.ShowModal; > finally > Form2.Free; > end; > end; > > procedure TForm1.MenuItem2Click(Sender: TObject); > begin > Form3 := TForm3.Create (Application); > try > Form3.ShowModal; > finally > Form3.Free; > end; > end; > > procedure TForm1.MenuItem3Click(Sender: TObject); > begin > Form4 := TForm4.Create (Application); > try > Form4.ShowModal; > finally > Form4.Free; > end; > end; > > initialization > {$I unit1.lrs} > > end. > > Unit2.pas > > unit Unit2; > > {$mode objfpc}{$H+} > > interface > > uses > Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus; > > > type > > { TForm2 } > > TForm2 = class(TForm) > MainMenu1: TMainMenu; > MenuItem1: TMenuItem; > procedure MenuItem1Click(Sender: TObject); > private > { private declarations } > public > { public declarations } > end; > > var > Form2: TForm2; > > implementation > > { TForm2 } > > procedure TForm2.MenuItem1Click(Sender: TObject); > begin > Close; > end; > > initialization > {$I unit2.lrs} > > end. > > Regards. > > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus > > -- o__ ,_.>/ _ (_)_\(_)_______ ...speed is good _______________ I believe five out of four people have a problem with fractions.

------------------
Powered by NetMail

Reply by email

Bart
to Usuario Anónimo: Sometimes in a simple applicattion whith a principal form and several secondary forms…

Should you free the form if you set Application as the owner? Will not then Application try to free it again if the app closes, and throw an AV at you? If you do FreeAndNil() instead of Free, does it make any diffrence?

Try running the app from gbd, then do a backtrace after the AV.

Bart
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081002/4c99c280/attachment.html

Reply by email

Marc Weustink
to Bart
Bart wrote 5 lines
Should you free the form if you set Application as the owner? Will not then Application try to free it again if the app closes, and throw an AV at you?

No, when you free componentA which is owned by componentB, then componentA is removed from the childlist of componentB, avoiding a double free.

Marc

Reply by email

Usuario Anónimo
to Marc Weustink

project1.lpr

program project1;
{$mode objfpc}{$H+}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms
  { add your units here }, Unit1;
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
*I forgot to say that the error occurs under WinCE.*

The error not always occurs, only sometimes. Why?

2008/10/2 Marc Weustink

Show quoted text 18 lines
Bart wrote: > > > Should you free the form if you set Application as the owner? Will not > then Application try to free it again if the app closes, and throw an AV > at you? No, when you free componentA which is owned by componentB, then componentA is removed from the childlist of componentB, avoiding a double free. Marc _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081002/8ea03d62/attachment.html

Reply by email

Usuario Anónimo
to Usuario Anónimo

I think that the problem is the TMainMenu component. Now, I use a TPopupMenu and it works correctly.

2008/10/2 Usuario An?nimo

Show quoted text 42 lines
project1.lpr program project1; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset Forms { add your units here }, Unit1; begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end. *I forgot to say that the error occurs under WinCE.* The error not always occurs, only sometimes. Why? 2008/10/2 Marc Weustink Bart wrote: > > > > > > Should you free the form if you set Application as the owner? Will not > > then Application try to free it again if the app closes, and throw an AV > > at you? > > No, when you free componentA which is owned by componentB, then > componentA is removed from the childlist of componentB, avoiding a > double free. > > Marc > > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081003/039c445b/attachment.html

Reply by email