G
gywlily
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是测试程序的片段:
type
IFormattedNumber = interface
['{9C790B0D-176C-4B31-B0B6-969593DCA319}']
function GetName: string;
end;
TFormattedInteger = class(TInterfacedObject, IFormattedNumber)
public
Constructor Create;
protected
Function GetName: string;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure ShowName(Intf: IFormattedNumber);
public
{ Public declarations }
end;
{ TForm1 }
procedure TForm1.ShowName(Intf: IFormattedNumber);
begin
Memo1.Lines.Add(Intf.GetName)
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyInt: TFormattedInteger;
begin
MyInt := TFormattedInteger.Create;
ShowName(MyInt as IFormattedNumber); // ShowName(MyInt);
MyInt.Free;
end;
{ TFormattedInteger }
constructor TFormattedInteger.Create;
begin
inherited ;
end;
function TFormattedInteger.GetName: string;
begin
Result := 'TFormattedNumber.GetName';
end;
结果:程序第一次执行到Button1Click的MyInt.Free;时抛出异常。
但是如果把Button1Click中的ShowName(MyInt as IFormattedNumber)改为注后的 语句// ShowName(MyInt);时第一次执行Button1Click没问题,第二次执行Button1Click时抛出异常:著名的"Access Violation"
愚人之问:使用ShowName(MyInt as IFormattedNumber), 函数返回时MyInt已经free掉了,接口也Release了,但为何隐式接口转换(ShowName(MyInt))得等到第二次执行Button1Click时才产生异常??
请大家讨论
type
IFormattedNumber = interface
['{9C790B0D-176C-4B31-B0B6-969593DCA319}']
function GetName: string;
end;
TFormattedInteger = class(TInterfacedObject, IFormattedNumber)
public
Constructor Create;
protected
Function GetName: string;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure ShowName(Intf: IFormattedNumber);
public
{ Public declarations }
end;
{ TForm1 }
procedure TForm1.ShowName(Intf: IFormattedNumber);
begin
Memo1.Lines.Add(Intf.GetName)
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyInt: TFormattedInteger;
begin
MyInt := TFormattedInteger.Create;
ShowName(MyInt as IFormattedNumber); // ShowName(MyInt);
MyInt.Free;
end;
{ TFormattedInteger }
constructor TFormattedInteger.Create;
begin
inherited ;
end;
function TFormattedInteger.GetName: string;
begin
Result := 'TFormattedNumber.GetName';
end;
结果:程序第一次执行到Button1Click的MyInt.Free;时抛出异常。
但是如果把Button1Click中的ShowName(MyInt as IFormattedNumber)改为注后的 语句// ShowName(MyInt);时第一次执行Button1Click没问题,第二次执行Button1Click时抛出异常:著名的"Access Violation"
愚人之问:使用ShowName(MyInt as IFormattedNumber), 函数返回时MyInt已经free掉了,接口也Release了,但为何隐式接口转换(ShowName(MyInt))得等到第二次执行Button1Click时才产生异常??
请大家讨论