1.在构造函数中把引用计数初始化为1
HRESULT _stdcall IClassFactory::CreateInstance(IUnknown * pUnkOuter,REFIID riid,void ** ppvObject)
{
pObject = new CObject();//此时m_dwRef为一
HRESULT hr = if(pObject) pObject->QueryInterface(riid, ppvObject);
pObject->Release(); //不管查找接口是否成功,都去release(),, 如果没找到,进行自删除
return hr;
}
2.在构造函数中把引用计数初始化为0
HRESULT CDictionaryFactory::CreateInstance(IUnknown *pUnknownOuter,
const IID& iid, void **ppv)
{
CDictionary * pObj = new CDictionary();;
HRESULT hr;
if (NULL==pObj)
return hr;
hr=pObj->QueryInterface(iid, ppv);
if (hr != S_OK) {
delete pObj; //如果没找到对象直接删除
}
两种方法都是com内部实现,,与外部使用者无关...也就是说使用完全一样