Reference counting in Delphi

From OPC Labs Knowledge Base
Revision as of 15:58, 14 February 2018 by User (talk | contribs) (Created page with "Category:Delphi When using QuickOPC components from Delphi, you are effectively making use of COM (sometimes also referred to ActiveX, OLE, or Automation) objects. The lif...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

When using QuickOPC components from Delphi, you are effectively making use of COM (sometimes also referred to ActiveX, OLE, or Automation) objects. The lifetime (including memory allocation) of COM objects is controlled by reference counting. In short, anybody who uses COM component needs to properly call the AddRef() and Release() methods on any COM object it works with. As long as the reference count maintained by these methods is non-zero, the COM object stays as alive. When the reference count goes to zero, the object is disposed of.

Failure to do the reference counting correctly results in bugs that are sometimes very hard to diagnose. Symptoms of such bugs may be objects "disappearing" for unknown reason, or memory leaks.

QuickOPC is not special in respect to COM reference counting, and the rules that apply to QuickOPC usage in Delphi are the general rules that apply to any COM component. We have, however, put together a short set of recommendations that should help you to write bug-free code more easily, in this respect. Here they are: