I am working on Delphi 7. I am getting access violation error while freeing the form.
1) Create new Application Delphi 7 (Unit1)
2) Add new form (Unit2)
3) For Unit1 uses clause add Unit2 and write below code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, unit2, StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
form2 : TForm2;
begin
try
form2 := TForm2.Create(Application);
//form2.ShowModal;
finally
//FreeAndNil(form2);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Form2 <> nil then
FreeAndNil(form2);
end;
end.
4) Run the Application and close Form1 -- Getting below error.
I don't want to free Form2 Object on OnClose or OnCloseQuery, Because i have some code on these events.I want to free Form2 object after OnClose or OnCloseQuery.
why i am getting error and how to fix this?
Form2
is unknown, where did you declare it ? where did you created it ? From the code you posted this should not even compile – GuidoGForm2
is probably declared inunit2
and never created in this unit. Either remove the declaration ofForm2
from your formCreate here, or move it to the private section of this form and remove the declaration inunit2
– GuidoG