Kernel SDK Developer's Guide > Basic Operations > Working with Smart Pointers > Example of Counting References
Example of Counting References

This example demonstrates the counting of references for static and dynamic instances created by either an empty or standard implementation of the reference counter. The example uses the console application project in the following implementation:

#include "conio.h"
#include "OdaCommon.h"
#include "OdToolKit.h"
#include "..\..\Kernel\Extensions\ExServices\ExHostAppServices.h"
#include "..\..\Kernel\Extensions\ExServices\ExSystemServices.h"

class MyApp : public ExSystemServices 
{
 protected:
   ODRX_USING_HEAP_OPERATORS(ExSystemServices);

 public:
   MyApp() {};
};

void TestStaticInstance();
void TestDynamicInstance();

int main()
{
   OdStaticRxObject<MyApp> svcs;

   odInitialize(&svcs);

   odPrintConsoleString(L"\nCreating the static instance");
   TestStaticInstance();
   odPrintConsoleString(L"\nReturn from Test");
   getch();

   odPrintConsoleString(L"\n\n");

   odPrintConsoleString(L"\nCreating the dynamic instance");
   TestDynamicInstance();
   odPrintConsoleString(L"\nReturn from Test");
   getch();

   odUninitialize();

   return 0;
}

The example defines the TestStaticInstance() function for testing instances created statically and the TestDynamicInstance() function for testing instances created dynamically. Both functions call the RefCountTest() function, which gets the raw pointer to the instance of the user-defined class (MyObj) derived from the OdRxObject, and both functions create new smart pointers to them, assign the address of the instance (or NULL) to them, and print the reference counter after assignment operators. The RefCountTest() function has the following implementation:

void RefCountTest(MyObj* pMy)
{
   MyObjPtr pSmart1 = pMy;
   odPrintConsoleString(L"\n1.(pMy)->(pSmart1)\n  Reference counter: %d\n", pMy->numRefs());
   getch();

   MyObjPtr pSmart2 = pSmart1;
   MyObjPtr pSmart3 = pSmart2;
   odPrintConsoleString(L"\n2.(pSmart1)->(pSmart2),(pSmart3)\n  Reference counter: %d\n", pMy->numRefs());
   getch();

   pSmart2 = NULL;
   odPrintConsoleString(L"\n3.(pSmart2)=NULL\n  Reference counter: %d\n", pMy->numRefs());
   getch();

   MyObjPtr pSmart4 = pSmart3;
   MyObjPtr pSmart5 = pSmart4;
   odPrintConsoleString(L"\n4.(pSmart3)->(pSmart4),(pSmart5)\n  Reference counter: %d\n", pMy->numRefs());
   getch();

   pSmart1 = NULL;
   pSmart4 = NULL;
   odPrintConsoleString(L"\n5.(pSmart1)=NULL, (pSmart4)=NULL\n  Reference counter: %d\n", pMy->numRefs());
   getch();

   pSmart1 = NULL;
   pSmart3 = NULL;
   odPrintConsoleString(L"\n6.(pSmart1)=NULL, (pSmart3)=NULL\n  Reference counter: %d\n", pMy->numRefs());
   getch();
}

The implementation of the TestStaticInstance() and TestDynamicInstance() functions depends on the definition of the user-defined class (MyObj) that can be derived either from the empty implementation using the OdStaticRxObject class or the standard implementation using the OdRxObjectImpl class.

See Also

Working with Smart Pointers

Using an Empty Implementation of the Reference Counter

Using a Standard Implementation of the Reference Counter

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.