D
DreamTiger
Unregistered / Unconfirmed
GUEST, unregistred user!
最近写一个程序,想尝试interface模式,但是被他的释放过程搞得头昏脑胀,不知道在delphi中是否建议不要使用TInterfacedObject?
unit Unit1
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls
type
TForm1 = class(TForm)
Memo1: TMemo
Button1: TButton
procedure Button1Click(Sender: TObject)
private
{ Private declarations }
public
{ Public declarations }
end
var
Form1: TForm1
implementation
{$R *.dfm}
type
IIntf = interface
['{714D88EA-4825-44B7-AD0B-027AA0B1D6B1}']
procedure Print
end
TIntf = class(TInterfacedObject, IIntf)
public
procedure Print
constructor Create
destructor Destroy;override
end
{ TIntf }
constructor TIntf.Create
begin
Form1.Memo1.Lines.Add('TIntf.Create')
end
destructor TIntf.Destroy
begin
Form1.Memo1.Lines.Add('TIntf.Destroy')
inherited
end
procedure TIntf.Print
begin
Form1.Memo1.Lines.Add('TIntf.Print')
end
procedure TForm1.Button1Click(Sender: TObject)
var
intf:IIntf
imp:TIntf
begin
imp := TIntf.Create
Form1.Memo1.Lines.Add('after imp := TIntf.Create, refcount := ' + IntToStr(imp.RefCount))
intf := imp as IIntf
Form1.Memo1.Lines.Add('after intf := imp as IIntf, imp refcount := ' + IntToStr(imp.RefCount))
intf.Print
intf := nil
Form1.Memo1.Lines.Add('after intf := nil, imp refcount := ' + IntToStr(imp.RefCount))
imp.Print
intf := imp as IIntf
//出错
end
end.
输出结果:
TIntf.Create
after imp := TIntf.Create, imp refcount = 0
after intf := imp as IIntf, imp refcount = 1
TIntf.Print
TIntf.Destroy
after intf := nil, imp refcount = 20
TIntf.Print
搞不懂:
1、intf := nil后,TIntf就被Destroy了;
2、refcount怎么就变成20了。
3、都调用过了TIntf.Destroy,还能调用TInf.Print,只能说是内存还没有被覆盖了。
晕阿。
unit Unit1
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls
type
TForm1 = class(TForm)
Memo1: TMemo
Button1: TButton
procedure Button1Click(Sender: TObject)
private
{ Private declarations }
public
{ Public declarations }
end
var
Form1: TForm1
implementation
{$R *.dfm}
type
IIntf = interface
['{714D88EA-4825-44B7-AD0B-027AA0B1D6B1}']
procedure Print
end
TIntf = class(TInterfacedObject, IIntf)
public
procedure Print
constructor Create
destructor Destroy;override
end
{ TIntf }
constructor TIntf.Create
begin
Form1.Memo1.Lines.Add('TIntf.Create')
end
destructor TIntf.Destroy
begin
Form1.Memo1.Lines.Add('TIntf.Destroy')
inherited
end
procedure TIntf.Print
begin
Form1.Memo1.Lines.Add('TIntf.Print')
end
procedure TForm1.Button1Click(Sender: TObject)
var
intf:IIntf
imp:TIntf
begin
imp := TIntf.Create
Form1.Memo1.Lines.Add('after imp := TIntf.Create, refcount := ' + IntToStr(imp.RefCount))
intf := imp as IIntf
Form1.Memo1.Lines.Add('after intf := imp as IIntf, imp refcount := ' + IntToStr(imp.RefCount))
intf.Print
intf := nil
Form1.Memo1.Lines.Add('after intf := nil, imp refcount := ' + IntToStr(imp.RefCount))
imp.Print
intf := imp as IIntf
//出错
end
end.
输出结果:
TIntf.Create
after imp := TIntf.Create, imp refcount = 0
after intf := imp as IIntf, imp refcount = 1
TIntf.Print
TIntf.Destroy
after intf := nil, imp refcount = 20
TIntf.Print
搞不懂:
1、intf := nil后,TIntf就被Destroy了;
2、refcount怎么就变成20了。
3、都调用过了TIntf.Destroy,还能调用TInf.Print,只能说是内存还没有被覆盖了。
晕阿。