未知内存泄漏形式 delphi xe8

Unknown memory leak form delphi xe8

我创建了一个简单的应用程序,它在表单 Tform1 上有一个按钮,用于创建另一个表单 Tform2。 Form2 包含一个工具栏,每个角有 2 个按钮,下面有一个标签和一个 tabcontrol。我一直在下面看到这个内存泄漏。我确定我正确地创建和销毁了表格。

Screenshot of error

program Project1;

uses
  System.StartUpCopy,
  FMX.Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
   ReportMemoryLeaksOnShutdown := True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

这里是创建 form2 的 form1

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, Unit2;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
        Hide;
        Application.CreateForm(TForm2, Form2);
        Form2.Show;
        Form2.WindowState := TWindowState.wsMaximized;
end;

end.

带有工具栏和选项卡控件的表单 2

Form2 layout pic

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.TabControl, FMX.Controls.Presentation;

type
  TForm2 = class(TForm)
    ToolBar1: TToolBar;
    Button1: TButton;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    TabItem2: TTabItem;
    Label1: TLabel;
    Button2: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := TCloseAction.caFree;
  // ShowMessage('Form Freed now closing whole application') ;
  Application.MainForm.Close;
end;

end.

正如我在评论中所说,我无法用您显示的代码重现内存泄漏报告。但也许你追错了鹅,所以我会尝试一些猜测。

您从未解释过为什么要这样对待 Form1,但我有一种奇怪的感觉,它可能是一个登录表单。如果是这样的话,我会这样做:

Project1.dpr

program Project1;

uses
  System.StartUpCopy,
  FMX.Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  System.UITypes;  // for the modal result

{$R *.res}

begin
  ReportMemoryLeaksOnShutdown := True;
  Application.Initialize;

  Form1 := TForm1.Create(Application);
  try
  if Form1.ShowModal <> System.UITypes.mrOK then
    Exit;
  finally
    Form1.Free;
  end;

  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

请注意 Form1 而不是 使用 Application.CreateForm 创建的。 原因是用 Application.CreateForm 创建的 first 表单成为应用程序的主要表单,关闭它会关闭应用程序。相反,我们让主要 UI 所在的 Form2 成为主窗体,这样我们就可以释放登录后不需要的 Form1

现在我们还需要设置登录表单 (Form1) 的模态结果,例如(但您会希望它更安全):

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (Edit1.Text = 'User') and (Edit2.Text = 'pass') then
    modalresult := System.UITypes.mrOK;
end;

如果这根本不是您要查找的内容,请告诉我,我会删除。