第四章 引用计数
这章引出了一个控制权的问题,谁将控制组件的生存期,落到语言层面就是,谁将负责new和delete.在所有组件都要继承的IUnknown接口中AddRef和Release这两个成员负责了组件生命期的管理.从而可以去掉客户代码中的delete语句.
对客户而言,它并不知道COM组件的细节,以前觉得COM编程会给用户提供一个头文件,但现在看来不是如此,客户必须使用QueryInterface来查询COM有哪些接口,这有有一个问题,客户应该给COM组件一个什么样的搜索条件呢,也就是客户是如何知道每个IID对应的接口的功能和调用方法呢?我想随着时间的推移,这个问题应该会逐步明晰,现在还是先来看引用计数.
AddRef和Release实现的是一种名为引用计数的内存管理技术.引用计数的内存管理技术.引用计数是使组件能够自己将自己删除的最简单同时也是效率最高的方法.COM组件将维护一个称作是引用计数的数值.当客户从组件取得一个接口时,此引用计数值将增1.当客户使用完某个接口后,组件的引用计数值将减1.当引用计数值为0时,组件即可将自己从内存中删除.
对每个接口维护一个引用计数将更方便调试.更有利于实现资源的分配,因为可能出现这样一种情况,在实现某个接口时可能需要大量的内存或其他资源,可以在QueryInterface的实现中,在客户请求此接口时完成资源的分配,但若只对整个组件维护一个引用计数,组件将无法决定何时可以安全地将同此接口相关联的内存释放.但下面的例子是为了简化起见,对整个组件维护单个的引用计数值,下面是实例代码:
接口类 interface.h
#include
<
objbase.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
Interfaces
interface
IX:IUnknown
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
virtual void _stdcall Fx() = 0;
}
;
interface
IY:IUnknown
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
virtual void _stdcall Fy() = 0;
}
;
interface
IZ:IUnknown
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
virtual void _stdcall Fz() = 0;
}
;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
IIDs
static
const
IID IID_IX
=
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
0x32bb8320,0xb41b,0x11cf,...{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82}
}
;
static
const
IID IID_IY
=
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
0x32bb8321,0xb41b,0x11cf,...{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82}
}
;
static
const
IID IID_IZ
=
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
0x32bb8322,0xb41b,0x11cf,...{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82}
}
;
实现类 --头文件CB.h
#ifndef CB_H
#define
CB_H
#include
"
interface.h
"
#include
<
iostream.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
extern
void
trace(
const
char
*
msg);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
Creation function
extern
IUnknown
*
CreateInstance();
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
Component
class
CB:
public
IX,
public
IY
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
virtual HRESULT _stdcall QueryInterface(const IID& iid, void** ppv);
virtual ULONG _stdcall AddRef();
virtual ULONG _stdcall Release();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//interfac IX implementation
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
virtual void _stdcall Fx()...{cout<<"Fx()"<<endl;}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//interfac IY implementation
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
virtual void _stdcall Fy()...{cout<<"Fy()"<<endl;}
public:
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
CB():m_cRef(0)...{};
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
~CB()...{trace("CB: Destroy Self.");}
private:
long m_cRef;
}
;![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#endif
实现类--CB.cpp
#include
"
CB.h
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
HRESULT _stdcall CB::QueryInterface(
const
IID
&
iid,
void
**
ppv)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
if (iid == IID_IUnknown)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("QuerInterface:Return pointer to IUnknown");
*ppv = static_cast<IX*>(this);
}
else if(iid == IID_IX)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("QueryInterface: Return pointer to IX.");
*ppv = static_cast<IX*>(this);
}
else if (iid == IID_IY)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("QueryInterface: Return pointer to IY.");
*ppv = static_cast<IY*>(this);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("QueryInterface: Interface not supported.");
*ppv = NULL;
return E_NOINTERFACE;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
ULONG _stdcall CB::AddRef()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
cout<<"CB: AddRef = "<<m_cRef+1<<'.'<<endl;
return InterlockedIncrement(&m_cRef);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
ULONG _stdcall CB::Release()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
cout<<"CB: Release = "<<m_cRef-1<<'.'<<endl;
if (InterlockedDecrement(&m_cRef) == 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
delete this;
return 0;
}
return m_cRef;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
trace(
const
char
*
msg)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
cout<<msg<<endl;
}
//
Creation function
IUnknown
*
CreateInstance()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
IUnknown* pI = static_cast<IX*>(new CB);
pI->AddRef();
return pI;
}
客户端代码--App.cpp
#include
"
CB.h
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
HRESULT hr;
trace("Client: Get an IUnknown pointer.");
IUnknown* pIUnknown = CreateInstance();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
trace("Client: Get Interface IX.");
IX *pIX = NULL;
hr = pIUnknown->QueryInterface(IID_IX,(void**)&pIX);
if (SUCCEEDED(hr))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("Client: Succeeded getting IX.");
pIX->Fx();
pIX->Release();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
trace("Client: Get Interface IY.");
IY *pIY = NULL;
hr = pIUnknown->QueryInterface(IID_IY,(void**)&pIY);
if (SUCCEEDED(hr))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("Client: Succeeded getting IY.");
pIY->Fy();
pIY->Release();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
trace("Client: Ask for an unsuppoered interface.");
IZ *pIZ = NULL;
hr = pIUnknown->QueryInterface(IID_IZ,(void**)&pIZ);
if (SUCCEEDED(hr))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("Client: Succeeded getting IZ.");
pIZ->Fz();
pIZ->Release();
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace("Client: Could not get interface IZ.");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
trace("Client: Release IUnknown interface.");
pIUnknown->Release();
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
/*输出结果
Client: Get an IUnknown pointer.
CB: AddRef = 1.
Client: Get Interface IX.
QueryInterface: Return pointer to IX.
CB: AddRef = 2.
Client: Succeeded getting IX.
Fx()
CB: Release = 1.
Client: Get Interface IY.
QueryInterface: Return pointer to IY.
CB: AddRef = 2.
Client: Succeeded getting IY.
Fy()
CB: Release = 1.
Client: Ask for an unsuppoered interface.
QueryInterface: Interface not supported.
Client: Could not get interface IZ.
Client: Release IUnknown interface.
CB: Release = 0.
CB: Destroy Self.
Press any key to continue
*/
在上面的代码我们可以发现,把子类的对象赋值给父类的指针是有道理的,从面向对象的思想来看,继承一方面是为了共用,另一方面是为了保证接口稳定性.从COM编程的思想来看,继承主要是为了实现多态,保证接口稳定性,可以从COM接口不包含数据成员可以看出(继承共用的也就是父类的数据成员).所以只要子类实现了父类的接口,我们就可以用父类的指针来操作子类的对象,也就是苹果一定是水果,所以我用水果来操作苹果肯定没错.下面的代码是本例中截取出来的,可见一斑:
IUnknown
*
pI
=
static_cast
<
IX
*>
(
new
CB);