[Lazarus] Exception when I change betwen forms

Mattias Gärtner nc-gaertnma at netcologne.de
Thu Oct 2 11:44:32 CEST 2008


Zitat von Reenen Laurie <rlaurie at gmail.com>:

> 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


> 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 <usuarioanonimomysql at gmail.com>
>
> > 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
> > Lazarus at lazarus.freepascal.org
> > 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



More information about the Lazarus mailing list