Output
When you build and run the refCount project, you should get the following output in a console window:
dm1: refCount(pDom ++) = 1 dm2: refCount(pRoot ++) = 1 dm3: refCount(pElem ++) = 1 dm4: refCount(pElemOut ++) = 2 dm5: refCount(pElemA ++) = 3 dm6: refCount(pElem --) = 2 dm7: refCount(pElemOut --) = 1 dm8: refCount(pElemA ) = 1 dm9: refCount(pElem ++) = 1 dm10: refCount(pElemOut ++) = 2 dm11: refCount(pElemA --) = 0 dm12: refCount(pElem --) = 1 dm13: refCount(pElemOut --) = 0 dm14: refCount(pElem ++) = 1 dm15: refCount(pElemOut ++) = 2 dm16: refCount(pElem --) = 1 dm17: refCount(pElemOut --) = 0 dm18: refCount(pElemOut ++) = 2 dm19: refCount(pElemOut --) = 1 pDom->xml: <root><A><a>11</a></A><B>2</B></root> dm20: refCount(pDom --) = 0 dm21: refCount(pRoot --) = 0
Remarks
The following remarks explain the various reference counts shown above. However, because reference counting is implementation-dependent, these discussions might or might not apply to other objects.
- Messages dm1: - dm3:
- Show three newly created objects:
pDom
, which corresponds to the document element;pRoot
, which corresponds to the<root>
element; andpElem
, which corresponds to the<A>
element. Because these objects are new and distinct, the reference count equals 1 on each interface pointer. - Message dm4:
- Shows the reference count of
pElemOut
as returned from the call topRoot->appendChild
method. BecausepElemOut
is another reference to thepElem
object, when the call succeeds its reference count is 1 plus the count onpElem
—that is, 2. - Message dm5:
- Shows the reference count of
pElemA
, which is a copy ofpElemOut
;pElemA
is used to hold the<A>
element, so thatpElem
andpElemOut
can be recycled to represent other elements. So far,pElem
,pElemOut
, andpElemA
all point to the same object. Therefore, the reference count onpElemA
is 3. - Messages dm6: - dm8:
- Show the reference counts on
pElem
,pElemOut
, andpElemA
after theRelease
method has been called onpElem
andpElemOut
. These two interface pointers need to be released, because they will be used to add another element,<a>
, to the XML document. Remember that at this point in the code all three interface pointers still reference the same object. This means that the reference counts on them are correlated with each other. Failure to releasepElem
orpElemOut
here will result in a memory leak. - Messages dm9:
- Shows the reference count on the
pElem
interface pointer, which points to a newly createdIXMLDOMNode
object. This object, which represents the<a>
element, is to be added as a child of<A>
. Because this is a newly created object, the reference count on this interface pointer is 1. - Message dm10:
- Shows the reference count of
pElemOut
as returned from the call topElemA->appendChild
method. This element is another reference to thepElem
object,<a>
. Therefore, when the call succeeds, its reference count is 1 plus the reference count onpElem
—that is, 2. - Messages dm11: - dm13:
- Show the reference counts on
pElemA
,pElem
, andpElemOut
after theRelease
method was called on each of the interface pointers. The system has releasedpElemA
because we are done with<A>
; when the reference count onpElemA
reaches zero, the system frees the memory object pointed to by thepElemA
interface pointer. Any attempt to access this object will result in access violation. The other two interface pointers are released because we are ready to move on to the next element,<B>
. Failing to releasepElem
andpElemOut
here would result in a memory leak. - Messages dm14:
- Shows the reference count on the
pElem
interface pointer, which points to a newly createdIXMLDOMNode
object,<B>
. This object is to be added as a child of<root>
. BecausepElem
is not a copy of another interface pointer, the reference count is 1. - Message dm15:
- Shows the reference count of
pElemOut
as returned from the call topRoot->appendChild
method. This element is another reference to thepElem
object,<B>
. Therefore, when the call succeeds, its reference count is 1 plus the reference count onpElem
—that is, 2. - Messages dm16: - dm17:
- Show the reference counts on
pElem
andpElemOut
after theRelease
method was called on each of the interface pointers. These two interface pointers are released because we are done with<B>
. Failing to releasepElemOut
here would result in memory leak. - Message dm18:
- Shows the reference count of
pElemOut
as returned from the call topDom->appendChild
method. This element is another reference to thepRoot
object,<root>
. Therefore, when the call succeeds, its reference count is 1 plus the reference count onpRoot
—that is, 2. - Messages dm19: - dm21:
- Show the reference counts on
pElemOut
,pDom
, andpRoot
after theRelease
method has been called on each of the interface pointers. These interface pointers are released because they are no longer needed. All the memory objects allocated in this application are freed when the application ends.