为什么居中的 MessageDlg 会产生异常?

Why does a centered MessageDlg create exceptions?

Delphi 6.
我实现了一个以所有者表单为中心的 MessageDlg
正如@David Heffernan 在 2011 年 1 月 6 日所建议的那样。

2011 年的原始问题在这里: How to make MessageDlg centered on owner form.

居中对话框有效一次。
第一次抛出异常后。
- 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;

异常在我的activity日志中显示如下:

Exception in CenteredMsgDlg: [EAccessViolation] Access violation at  
address 00000000. Read of address 00000000

CreateMessageDialog 创建以应用程序为所有者的表单 - 它被添加到应用程序组件列表中。使用 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;