且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不在我的 Delphi 对象上调用 _AddRef 和 _Release?

更新时间:2023-02-24 15:29:32

Refcount 仅在您分配给接口变量时被修改,而不是分配给对象变量.

Refcount is only modified when you assign to an interface variable, not to an object variable.

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, pchar( inttostr( c2.refcount)), '', 0); // just to see the value 
    //Now it DOES show Refcount = 1
end;