I'm really confused.
// 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;
It should show 1, but it shows 0. No matter how many assignments we'll perform, the value would not change! Why not?
Refcount is only modified when you assign to an interface variable, not to an object variable.
The compiler doesn't add in any ref-counting code if you assign it to a class type variable. The refcount was never even set to 1, much less 2.
You'll see the expected behavior if you declare
c1
andc2
asIInterface
instead ofTTestClass
.