Hi,
I have project where i am trying to create main application form at runtime.
Project runs, form is created, problem is after closing the form
If this is used, program does not end, but keeps running.
Form1 := TForm1.CreateNew(Application);
If this is used, memory error occures (see attachment).
Application.CreateForm(TForm1, Form1);
Form does not use resources.
Main *.lpr code:
program Test;
{$mode objfpc}{$H+}
uses
Forms, Interfaces,
TestForm in 'TestForm.pas';
{$R *.res}
begin
RequireDerivedFormResource := False;
Application.Initialize;
CreateTestForm;
Application.Run;
end.
TestForm code:
unit TestForm;
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
StrUtils;
type
TForm1 = class(TForm)
private
cboCommand: TComboBox;
btnRun: TButton;
mmoResult: TMemo;
procedure btnRunClick(Sender: TObject);
{$IFDEF FPC}
procedure frmClose(Sender: TObject; var CloseAction: TCloseAction);
{$ELSE}
procedure frmClose(Sender: TObject; var Action: TCloseAction);
{$ENDIF}
public
{ Public declarations }
constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
end;
var
Form1: TForm1;
procedure CreateTestForm;
implementation
procedure CreateTestForm;
begin
{$IFDEF FPC}
// creates memory error
Application.CreateForm(TForm1, Form1);
Form1.Show;
{$ELSE}
// delphi/under Lazarus program does not end
Form1 := TForm1.CreateNew(Application);
Form1.ShowModal;
{$ENDIF}
end;
constructor TForm1.CreateNew(AOwner: TComponent; Dummy: Integer = 0);
begin
inherited;
Self.OnClose := @frmClose;
cboCommand := TComboBox.Create(Self);
cboCommand.Parent := Self;
cboCommand.Items.Append('test command');
btnRun := TButton.Create(Self);
btnRun.Parent := Self;
btnRun.Caption := 'Run Command';
btnRun.OnClick := @btnRunClick;
mmoResult := TMemo.Create(Self);
mmoResult.Parent := Self;
end;
{$IFDEF FPC}
procedure TForm1.frmClose(Sender: TObject; var CloseAction: TCloseAction);
begin
CloseAction := caFree;
end;
// delphi stuff
{$ELSE}
procedure TForm1.frmClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
{$ENDIF}
procedure TForm1.btnRunClick(Sender: TObject);
begin
//
end;