为什么我的Delphi对象上没有调用_AddRef和_Release?
发布时间:2020-12-15 10:04:11 所属栏目:大数据 来源:网络整理
导读:我真的很困惑. // initial classtype TTestClass = class( TInterfacedObject) end;{...}// test procedureprocedure testMF();var c1,c2 : TTestClass;begin c1 := TTestClass.Create(); // create,addref c2 := c1; // addref c1 := nil; // refcount - 1
|
我真的很困惑.
// initial class
type
TTestClass =
class( TInterfacedObject)
end;
{...}
// test procedure
procedure testMF();
var c1,c2 : TTestClass;
begin
c1 := TTestClass.Create(); // create,addref
c2 := c1; // addref
c1 := nil; // refcount - 1
MessageBox( 0,pchar( inttostr( c2.refcount)),'',0); // just to see the value
end;
它应该显示1,但它显示0.无论我们将执行多少任务,价值都不会改变!为什么不? 解决方法
只有在分配给接口变量而不是对象变量时才会修改Refcount.
procedure testMF();
var c1,c2 : TTestClass;
Intf1,Intf2 : IUnknown;
begin
c1 := TTestClass.Create(); // create,does NOT addref
c2 := c1; // does NOT addref
Intf1 := C2; //Here it does addref
Intf2 := C1; //Here,it does AddRef again
c1 := nil; // Does NOT refcount - 1
Intf2 := nil; //Does refcount -1
MessageBox( 0,0); // just to see the value
//Now it DOES show Refcount = 1
end;
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
