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
pElemOutas returned from the call topRoot->appendChildmethod. BecausepElemOutis another reference to thepElemobject, 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;pElemAis used to hold the<A>element, so thatpElemandpElemOutcan be recycled to represent other elements. So far,pElem,pElemOut, andpElemAall point to the same object. Therefore, the reference count onpElemAis 3. - Messages dm6: - dm8:
- Show the reference counts on
pElem,pElemOut, andpElemAafter theReleasemethod has been called onpElemandpElemOut. 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 releasepElemorpElemOuthere will result in a memory leak. - Messages dm9:
- Shows the reference count on the
pEleminterface pointer, which points to a newly createdIXMLDOMNodeobject. 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
pElemOutas returned from the call topElemA->appendChildmethod. This element is another reference to thepElemobject,<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, andpElemOutafter theReleasemethod was called on each of the interface pointers. The system has releasedpElemAbecause we are done with<A>; when the reference count onpElemAreaches zero, the system frees the memory object pointed to by thepElemAinterface 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 releasepElemandpElemOuthere would result in a memory leak. - Messages dm14:
- Shows the reference count on the
pEleminterface pointer, which points to a newly createdIXMLDOMNodeobject,<B>. This object is to be added as a child of<root>. BecausepElemis not a copy of another interface pointer, the reference count is 1. - Message dm15:
- Shows the reference count of
pElemOutas returned from the call topRoot->appendChildmethod. This element is another reference to thepElemobject,<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
pElemandpElemOutafter theReleasemethod was called on each of the interface pointers. These two interface pointers are released because we are done with<B>. Failing to releasepElemOuthere would result in memory leak. - Message dm18:
- Shows the reference count of
pElemOutas returned from the call topDom->appendChildmethod. This element is another reference to thepRootobject,<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, andpRootafter theReleasemethod 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.
