Kbase 18800: ACTIVEX - How We Handle Object References (COM-HANDLES)
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  11/11/2008 |
|
Status: Verified
GOAL:
How We Handle Object References (COM-HANDLES)
FIX:
Progress keeps track of the distinct COM Objects (COM-HANDLES) used in a program and maintains the reference count contributed by Progress for each object at one (1). This means that a COM Object may have a total reference count of N because it's being used by various programs but the contribution of the Progress session to N is always just one (1).
Using the example shown at the bottom of this solution
nodes1 = chtv:treeview:nodes
causes Progress to get the IDispatch for treeview:nodes. The TreeView OCX has already done an AddRef() on the IDispatch for "nodes" before giving it to us, so the reference count is one (1). Progress determines that it does not yet have a COM-HANDLE referencing the "nodes" object so it just stores the IDispatch and does nothing more.
Using the example shown at the bottom of this solution
nodes2 = chtv:treeview:nodes
causes Progress to get the IDispatch for treeview:nodes again. The TreeView OCX has already done an AddRef() on the IDispatch for "nodes" before giving it to us, so the reference count is now two (2).
However, this time, Progress discovers that it already has the IDispatch for "nodes" and calls Release() on the nodes IDispatch dropping the reference count back down to one (1).
This means that both nodes1 and nodes2 point to the same physical object.
Using the example shown at the bottom of this solution
release object nodes2
causes Progress to simply release its one (1) reference to "nodes" and removes it from the internal list it keeps of currently referenced COM Objects. This is why (as the documentation warns), doing a "RELEASE OBJECT" call invalidates all COM-HANDLES that reference the same object. The Progress "RELEASE OBJECT" statement is a lot more final than the COM Release() method.
Note also that when the COM-HANDLE goes out of scope, Progress does absolutely nothing with the object that the COM-HANDLE references.
That's why, if you have a COM-HANDLE as a local variable in a procedure and you have used it to access some COM Object that you do not want hanging around until the end of the Progress session, you need to explicitly call "RELEASE OBJECT" at the end of the procedure.
DEFINE VARIABLE nodes1 AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE nodes2 AS COM-HANDLE NO-UNDO.
nodes1 = chtv:treeview:nodes.
nodes2 = chtv:treeview:nodes.
MESSAGE "nodes1=" nodes1 SKIP "nodes2=" nodes2 VIEW-AS ALERT-BOX.
RELEASE OBJECT nodes2.
MESSAGE "is nodes1 valid? " VALID-HANDLE(nodes1) VIEW-AS ALERT-BOX.