Destroying a COM Object in Delphi

You have a .net assembly, calling it in delphi via COM.

var intf: ITest; ... intf:= CreateComObject(CLASS_TEST) as ITest; ... //here comes some stuff ... 

Do I have to do something to destroy it, to free memory. Or not?

+7
source share
4 answers

You better free up memory with

  intf := nil; 

If you don’t need it anymore. Better with try...finally intf := nil; or in Destroy override mode, if intf is defined as fIntf , that is, as a property of the class.

If intf defined on the stack, it will be automatically freed at the end of the method. There is a hidden try...finally intf := nil; end block try...finally intf := nil; end try...finally intf := nil; end generated by the compiler to free an intf instance.

+6
source

COM objects are counted by reference, and they are automatically destroyed when the reference count reaches zero. The compiler automatically adds calls to the _AddRef and _Release interface methods whenever your code adds an object reference or deletes it. Setting the variable referencing the COM object to nil will call _Release (decrease the reference count), and if the reference count reaches zero, the object will also be freed (it will not if the reference count is non-zero). When a variable goes out of scope (that is, a local variable when the procedure terminates), the compiler is also called _Release if the variable refers to a COM object (or any Delphi-linked interface).

+12
source

All COM interfaces must implement IUnknown :

 IUnknown = interface function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall; function _AddRef: Integer; stdcall; function _Release: Integer; stdcall; end; 

IUnknown provides two services. First of all, QueryInterface allows clients to get other interfaces that an object can implement. The second service is life cycle management.

Each time you refer to a COM object, you are responsible for calling _AddRef . Each time you refuse a reference to a COM object, you get a contract to call _Release .

The canonical implementation of _AddRef and _Release intended to ensure that the executing object maintains a reference counter variable, which increases and decreases when links are received and released. If the _Release call sets this reference count to 0, the object destroys itself.

The Delphi interface implementation manages the _AddRef and _Release on your behalf. When you assign an interface variable, the compiler emits code that:

  • Calls _Release on the interface the variable used to talk about if it really referred to something.
  • Calls _AddRef on the interface to which the variable now refers.

The compiler also arranges _Release for the call whenever the variable leaves the scope.

This means that you do not need to take any special steps to ensure that your COM objects are destroyed. Naturally, they will be destroyed when the last reference to the object leaves the area.

If, however, you want to destroy the object ahead of time, then you simply assign nil variable holding the interface. Please note that this of course assumes that no other links are supported on this interface.

+6
source

The object is released automatically. However, if you explicitly want to free the link that the intf variable intf , you can set it to nil.

+2
source

All Articles