the CComObjectRootEx

来源:互联网 发布:淘宝网制作费用 编辑:程序博客网 时间:2024/05/29 17:48
 

template <class ThreadModel>
class CComObjectRootEx : public CComObjectRootBase
{
public:
 typedef ThreadModel _ThreadModel;
 typedef typename _ThreadModel::AutoCriticalSection _CritSec;
 typedef typename _ThreadModel::AutoDeleteCriticalSection _AutoDelCritSec;
 typedef CComObjectLockT<_ThreadModel> ObjectLock;

 ~CComObjectRootEx() {}

 ULONG InternalAddRef()
 {
  ATLASSUME(m_dwRef != -1L);
  return _ThreadModel::Increment(&m_dwRef);
 }
 ULONG InternalRelease()
 {
#ifdef _DEBUG
  LONG nRef = _ThreadModel::Decrement(&m_dwRef);
  if (nRef < -(LONG_MAX / 2))
  {
   ATLASSERT(0 && _T("Release called on a pointer that has already been released"));
  }
  return nRef;
#else
  return _ThreadModel::Decrement(&m_dwRef);
#endif
 }

 HRESULT _AtlInitialConstruct()
 {
  return m_critsec.Init();
 }
 void Lock() {m_critsec.Lock();}
 void Unlock() {m_critsec.Unlock();}
private:
 _AutoDelCritSec m_critsec;
};

 

CComObjectRootEx handles object reference count management for both nonaggregated and aggregated objects. It holds the object reference count if your object is not being aggregated, and holds the pointer to the outer unknown if your object is being aggregated. For aggregated objects, CComObjectRootEx methods can be used to handle the failure of the inner object to construct, and to protect the outer object from deletion when inner interfaces are released or the inner object is deleted.

 

CComObjectRootex处理对象引用计数管理:无聚合与聚合。非聚合:保存对象引用计数;聚合:保存外部unknown的指针。对应聚合对象来说,CComOBjectRooex方法被用来处理内部对象的错误来创建并保护不被外部对象删除,当内部接口被释放或内部对象被删除时。

原创粉丝点击