You have to call DisposeOf()
no matter what the ModalResult
is. You are currently only calling it if the result is mrOk
. Do this instead:
procedure TForm1.Button1Click(Sender: TObject);
var
Form2: TForm2;
begin
Form2 := TForm2.Create(nil);
Form2.ShowModal(
procedure(ModalResult: TModalResult)
begin
if ModalResult = mrOK then
ShowMessage('OK')
else
ShowMessage('Cancel');
Form2.DisposeOf;
end
);
end;