Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Reference counting

Brew MP objects use reference counting and are released when the count is zero.

An object's reference count is updated at the following times:

  • When the object is instantiated by calling ISHELL_CreateInstance() or IEnv_CreateInstance(), the reference count of the object is incremented.
  • When a pointer to the object is retrieved using one of the xxxxx_QueryInterface() function calls, or other calls that return object pointers, the reference count of the object is incremented.
  • When the reference count is explicitly incremented or decremented using xxxxx_AddRef() or xxxxx_Release() function calls.

Most interfaces provide AddRef() and Release() functions, which can be used to increment or decrement the reference count, respectively. Applications should call the AddRef() function when they store a pointer to an object in the application's data structure and call the Release() function when the pointer is no longer needed.

Applications should also release all reference counted items in their FreeAppData() function. For example, if an application instantiates AEECLSID_SysClock and AEECLSID_ALARMMGR, as follows:

   if (ISHELL_CreateInstance(pMe->piShell, AEECLSID_SysClock,
               (void **)&pMe->piUserClock) != SUCCESS) {
      pMe->piUserClock = NULL;
      return FALSE;
   }
   if (ISHELL_CreateInstance(pMe->piShell, AEECLSID_ALARMMGR,
               (void **)&pMe->piAlarmMgr) != SUCCESS) {
      pMe->piAlarmMgr = NULL;
      return FALSE;
   }

The application's FreeAppData() function needs to free the resources for these objects, as follows:

void c_sample_FreeAppData(c_time_examples * pMe)
{
   if (pMe->piUserClock != NULL) {
      ISysClock_Release(pMe->piUserClock);
      pMe->piUserClock = NULL;

}
   if (pMe->piAlarmMgr != NULL) {
      IALARMMGR_Release(pMe->piAlarmMgr);
      pMe->piAlarmMgr = NULL;
   }
}