How to implement reference counting between AddRef and Release
This article is to share with you about AddRef and Release how to carry out citation counting, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it.
# include # include using namespace std
Interface IX: public IUnknown
{
Virtual void _ stdcall Fx () = 0
}
Interface IY: public IUnknown
{
Virtual void _ stdcall Fy () = 0
}
Interface IZ: public IUnknown
{
Virtual void _ stdcall Fz () = 0
}
Static const IID IID_IX = {0xec53e1de, 0x37d, 0x48f5, {0xa1, 0xee, 0x2d, 0xf5, 0xa5, 0xde, 0xf3, 0x49}}; / / {EC53E1DE-037D-48f5-A1EE-2DF5A5DEF349}
Static const IID IID_IY = {0x4c510b77, 0xc63a, 0x4e0f, {0xa2, 0x45, 0x7b, 0x21, 0x23, 0x12, 0x23, 0xe7}}; / / {4C510B77-C63A-4e0f-A245-7B21231223E7}
Static const IID IID_IZ = {0xefb1f8d4, 0x674e, 0x4123, {0xa9, 0x19, 0x3c, 0x72, 0xee, 0xe8, 0x1d, 0x92}}; / / {EFB1F8D4-674E-4123-A919-3C72EEE81D92}
Class CA: public IX
Public IY
{
Public:
CA (): m_cRef (0) {}
Virtual ~ CA () {}
Public:
Virtual HRESULT _ stdcall QueryInterface (REFIID riid, _ _ RPC__deref_out void * * ppvObject)
Virtual ULONG _ stdcall AddRef ()
Virtual ULONG _ stdcall Release ()
Virtual void _ stdcall Fx () {coutRelease (); / / need to correspond to these five places
}
IZ * pIZ = NULL; / / did not execute to AddRef, so no need to release
Hr = pIUnk- > QueryInterface (IID_IZ, (void**) & pIZ)
If (SUCCEEDED (hr))
{
Cout