Show: Delphi C++
Display Preferences

Using Reference Counting

From RAD Studio
Jump to: navigation, search

Go Up to Memory Management of Interface Objects

Note: This topic describes reference counting for interfaces, which is supported by all Delphi compilers. In addition, the Delphi mobile compilers support automatic reference counting for classes. For more information, see Automatic Reference Counting in Delphi Mobile Compilers.

The Delphi compiler provides most of the IInterface memory management for you by its implementation of interface querying and reference counting. Therefore, if you have an object that lives and dies by its interfaces, you can easily use reference counting by deriving from TInterfacedObject. If you decide to use reference counting, then you must be careful to only hold the object as an interface reference, and to be consistent in your reference counting. For example:

procedure beep(x: ITest);
function test_func()
  y: ITest;
  y := TTest.Create; // because y is of type ITest, the reference count is one
  beep(y); // the act of calling the beep function increments the reference count
  // and then decrements it when it returns
  y.something; // object is still here with a reference count of one

This is the cleanest and safest approach to memory management; and if you use TInterfacedObject it is handled automatically. If you do not follow this rule, your object can unexpectedly disappear, as demonstrated in the following code:

function test_func()
  x: TTest;
  x := TTest.Create; // no count on the object yet
  beep(x as ITest); // count is incremented by the act of calling beep
  // and decremented when it returns
  x.something; // surprise, the object is gone
Note: In the examples above, the beep procedure, as it is declared, increments the reference count (call _AddRef) on the parameter, whereas either of the following declarations do not:
procedure beep(const x: ITest);


procedure beep(var x: ITest);

These declarations generate smaller, faster code.

One case where you cannot use reference counting, because it cannot be consistently applied, is if your object is a component or a control owned by another component. In that case, you can still use interfaces, but you should not use reference counting because the lifetime of the object is not dictated by its interfaces.

See Also

Personal tools
RAD Studio 10.2 Tokyo
In other languages
Previous Versions