Delphi 下接口的疑问
delphi吧
全部回复
仅看楼主
level 7
OOPhappy 楼主
学习接口,按照说的,接口对象释放了,被引用的对象也会被释放,因此调用会出错。可是下面的类似代码却没有问题。有没有能帮我解惑的前辈。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
ITestInterface = interface
['{0DB7D472-B72B-4956-ABCC-AD67BB7C9517}']
function Res: Integer;
end;
TIntfObj = class(TObject,ITestInterface)
protected
FRefCount: Integer;
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
function Res(): Integer;
public
Num: Integer;
end;
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
aObject, bObject: TIntfObj;
aInterface: ITestInterface;
implementation
{$R *.dfm}
..................//省略若干
procedure TForm1.btn1Click(Sender: TObject);
begin
aObject := TIntfObj.Create;
aInterface:=aObject;
bObject:=TIntfObj.Create;
aInterface:=bObject;
aObject.Num := 2;
aInterface:= nil;
ShowMessage(IntToStr(aObject.Res)); ///////疑问。不是应该出错吗?
FreeAndNil(aObject); //出错,为什么前面的不出错,后面却出错。
end;
function TIntfObj.Res: Integer;
begin
Result := Num;
end;
end.
2015年02月07日 08点02分 1
吧务
level 14
那是因为僵尸指针指向的内存还没被覆盖而已
2015年02月07日 10点02分 2
1