为什么集中的MessageDlg会创建异常?

And*_*rsJ 4 delphi delphi-6

Delphi 6.

在2011年1月6日@David Heffernan的建议下实现了一个以所有者形式为中心的MessageDlg .

2011年的原始问题在这里: 如何使MessageDlg以所有者形式为中心.

居中对话框有效一次.
在第一次抛出异常之后.
- EAccessViolation
- 地址00000000处的访问冲突
- 读取地址00000000

我可能做错了什么?

function TEthernetNodes_form.CenteredMessageDlg(const Msg: string;
                                                DlgType:   TMsgDlgType;
                                                Buttons:   TMsgDlgButtons;
                                                HelpCtx:   Integer): Integer;
// Open a message Dialog in the center of the owner form
var
  Dialog: TForm;
begin
  Result := mrNo; // Suppress linker warning
  try
    Dialog := CreateMessageDialog(Msg, DlgType, Buttons);
    try
      Self.InsertComponent(Dialog);
      Dialog.Position := poOwnerFormCenter;
      Result := Dialog.ShowModal
    finally
      Dialog.Free
    end;

  except on E: Exception do
               begin
                 AddToActivityLog('Exception in CenteredMsgDlg: [' +  
                                   string(E.ClassName) + ']' +  
                                   E.Message, True, True);  
                 //Tried "ShowMEssage" instead of AddToActivityLog here. Does not display.
               end;

  end;
end;  

procedure TEthernetNodes_form.Button1Click(Sender: TObject);
begin
  CenteredMessageDlg('Test CenteredMessageDlg.', mtConfirmation, [mbOK], 0);
end;
Run Code Online (Sandbox Code Playgroud)

我的活动日志中显示了异常,如下所示:

Exception in CenteredMsgDlg: [EAccessViolation] Access violation at  
address 00000000. Read of address 00000000
Run Code Online (Sandbox Code Playgroud)

Dal*_*kar 6

CreateMessageDialog使用Application作为其所有者创建表单 - 将其添加到"应用程序组件"列表中.有了Self.InsertComponent(Dialog); 你将它添加到您的表单组件列表中,但它不是从应用程序的删除.

var
  Dialog: TForm;
begin
  Result := mrNo; // Suppress linker warning
  try
    Dialog := CreateMessageDialog(Msg, DlgType, Buttons);
    try
      Application.RemoveComponent(Dialog); // remove Dialog from Application components
      Self.InsertComponent(Dialog);
      Dialog.Position := poOwnerFormCenter;
      Result := Dialog.ShowModal;
    finally
      Dialog.Free
    end;
Run Code Online (Sandbox Code Playgroud)