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; // This is the problem (I think)
MenuItem1: TMenuItem; // This is the problem (I think)
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.
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.
Regards.
EDIT:
I think that the problem is the TMainMenu component. Now, I use a TPopupMenu and it works correctly.