[Solved] Creating main application form at runtime lazarus / 31-LCL ---------------------------------------------------------------------- vejrous · 2018-02-05 10:47 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. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Form1 := TForm1.CreateNew(Application); If this is used, memory error occures (see attachment). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Application.CreateForm(TForm1, Form1); Form does not use resources. Main *.lpr code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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; ---------------------------------------------------------------------- howardpc · 2018-02-05 12:35 For Lazarus you have to do some variation on this: The project's .lpr: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program ResourcelessMainForm; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Interfaces, Forms, uResourcelessForm; {$R *.res} begin  RequireDerivedFormResource:=True;  Application.Initialize;  CreateResourcelessMainForm;  Application.Run;end. The project's main form unit (no .lfm at all): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit uResourcelessForm; {$mode objfpc}{$H+} interface uses  Classes, Forms, StdCtrls, Controls; type   TResourcelessForm = class(TForm)  private  cboCommand: TComboBox;    btnRun: TButton;    mmoResult: TMemo;    procedure btnRunClick(Sender: TObject);    procedure SetupGUI;  protected    procedure DoClose(var CloseAction: TCloseAction); override;  public    constructor Create(TheOwner: TComponent); override;  end;   procedure CreateResourcelessMainForm; implementation procedure CreateResourcelessMainForm;var  mainFrm: TResourcelessForm;begin  mainFrm:=TResourcelessForm.Create(Application);  mainFrm.Show;end; { TResourcelessForm } constructor TResourcelessForm.Create(TheOwner: TComponent);begin  inherited CreateNew(TheOwner);  Caption := 'Resourceless main form';  SetupGUI;end; procedure TResourcelessForm.btnRunClick(Sender: TObject);begin  if cboCommand.Text <> '' then    mmoResult.Append(cboCommand.Text);end; procedure TResourcelessForm.DoClose(var CloseAction: TCloseAction);begin  CloseAction:=caFree;  inherited DoClose(CloseAction);  Application.Terminate;end; procedure TResourcelessForm.SetupGUI;begin  cboCommand := TComboBox.Create(Self);  with cboCommand do begin    Items.Append('test command');    AnchorSideTop.Control := Self;    AnchorSideLeft.Control := Self;    BorderSpacing.Around := 10;    Parent := Self;  end;   btnRun := TButton.Create(Self);  with btnRun do begin    Caption := 'Run Command';    AutoSize := True;    AnchorSideTop.Control := cboCommand;    AnchorSideTop.Side := asrBottom;    AnchorSideLeft.Control := Self;    BorderSpacing.Around := 10;    OnClick := @btnRunClick;    Parent := Self;  end;   mmoResult := TMemo.Create(Self);  with mmoResult do begin    AnchorSideTop.Control := Self;    AnchorSideLeft.Control := cboCommand;    AnchorSideLeft.Side:=asrRight;    BorderSpacing.Around := 10;    Parent := Self;  end; end; end. ---------------------------------------------------------------------- vejrous · 2018-02-05 13:36 @howardpc Many thanks for help, I belive it works now. ---------------------------------------------------------------------- RAW · 2018-02-05 18:19 QuoteIf this is used, memory error occures (see attachment). What's the matter with "0 unfreed memory blocks: 0" ??? Looks like a normal heaptrc-window to me.... This would be my first try ...  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PROGRAM MyMainForm;{$MODE OBJFPC}{$H+}{$J-}  USES  Interfaces,  Forms,  uMain; Procedure CreateMainWindow; Begin  wndMain:= TForm.Create(Application);   Try    wndMain.ShowModal;   Finally    wndMain.Release;    wndMain:= Nil;   End; End; BEGIN  RequireDerivedFormResource:= True;  Application.Initialize;  CreateMainWindow;END. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i UNIT uMain;{$MODE OBJFPC}{$H+} Interface USES  Classes, SysUtils, Forms, Controls;  VAR  wndMain: TForm; Implementation END. ---------------------------------------------------------------------- RAW · 2018-02-05 19:50 I recognized a weird TaskbarButton behaviour with "ShowModal". With "Show" everything looks normal...  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PROGRAM MyMainForm;{$MODE OBJFPC}{$H+}{$J-}  USES  Interfaces,  Forms,  uMain; BEGIN Application.Initialize;  wndMain               := TMainForm.Create(Application);   wndMain.ShowInTaskBar:= stAlways;   wndMain.Caption      := 'MainWindow';   wndMain.OnClose      := @uMain.wndMain.wndMainClose;   uMain.wndMain.wndMainSetup;   wndMain.Show; Application.Run;END. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i UNIT uMain;{$MODE OBJFPC}{$H+}{$J-} Interface USES  Classes, SysUtils, Forms, Controls, StdCtrls, Dialogs;  TYPE  TMainForm = Class(TForm)   Procedure wndMainClose (Sender: TObject; Var CloseAction: TCloseAction);   Procedure MemoClick    (Sender: TObject);   Procedure wndMainSetup;     PRIVATE     Memo: TMemo;  End;  VAR  wndMain: TMainForm; Implementation  Procedure TMainForm.wndMainSetup; Begin  Memo        := TMemo.Create(Self);  Memo.OnClick:= @MemoClick;  Memo.Parent := Self; End;  Procedure TMainForm.MemoClick(Sender: TObject); Begin  ShowMessage('Memo'); End;  Procedure TMainForm.wndMainClose(Sender: TObject; Var CloseAction: TCloseAction); Begin  Application.Terminate; End; END. ---------------------------------------------------------------------- vejrous · 2018-02-06 08:25 I ques memory is ok. Thought heaptrc window should look like this in ideal state (see attachement).