Reference count, addref, and release

Source: Internet
Author: User

The other two methods of iunknown:

The addref and release methods can be used to effectively manage the life cycle of components.

I. Three Rules for reference counting

1. Call addref when returning the interface pointer

IUnknown* CreateInstance(){    IUnknown* pI = static_cast<IX*>(new CA) ;    pI->AddRef() ;    return pI ;}

2. Call release when the interface is used up. The user is very lazy and always wants to let the system release resources by themselves. However, only the user can know when it is not required.

Remember to call release after the interface is used up

The following code

int main(){    HRESULT hr ;    trace("Client: Get an IUnknown pointer.") ;    IUnknown* pIUnknown = CreateInstance() ;    trace("Client: Get interface IX.") ;    IX* pIX = NULL ;     hr = pIUnknown->QueryInterface(IID_IX, (void**)&pIX) ;    if (SUCCEEDED(hr))    {        trace("Client: Succeeded getting IX.") ;        pIX->Fx() ;          // Use interface IX.        pIX->Release() ;    }    trace("Client: Get interface IY.") ;    IY* pIY = NULL ;    hr = pIUnknown->QueryInterface(IID_IY, (void**)&pIY) ;    if (SUCCEEDED(hr))    {        trace("Client: Succeeded getting IY.") ;        pIY->Fy() ;          // Use interface IY.        pIY->Release() ;    }    trace("Client: Ask for an unsupported interface.") ;    IZ* pIZ = NULL ;    hr = pIUnknown->QueryInterface(IID_IZ, (void**)&pIZ) ;    if (SUCCEEDED(hr))    {        trace("Client: Succeeded in getting interface IZ.") ;        pIZ->Fz() ;        pIZ->Release() ;    }    else    {        trace("Client: Could not get interface IZ.") ;    }    trace("Client: Release IUnknown interface.") ;    pIUnknown->Release() ;    return 0;}

3. Call addref.

 

pIX->Fx() ;          // Use interface IX.IX* pIX2=pIX;pIX2->AddRef();pIX2->Fx();pIX2->Release() ;pIX->Release() ;

Pointers nested in the life cycle of pointers that reference the same interface can not be referenced and counted. In a function, you do not need to reference and count the interface pointers that exist in local variables. Because the life cycle of a local variable is the same as that of a function, it will also be included in the life cycle of the caller. However, when copying a pointer from a global variable or to a global variable, You need to reference and count the pointer because the global variable can be released from any part of the function.

Generally, the customer must maintain a separate reference count value for each interface.

The rules for summarizing the reference count are as follows:

(1)Output parameter rules. Any function that returns a new interface pointer in the output parameter (such as the void ** BPPV of QueryInterface) or as the return value must call addref for this interface pointer. That is, the addref is called before the return.

(2)Output parameter rules. You do not need to call addref or release to pass in function interface pointers in input parameters (parameters or constants passed by C ++. Because the life cycle of a function is nested in the life cycle of the caller.

(3)Input-output parameter rulesThat is, you can use the input-output parameter value in the function body, and then modify these parameters and return them to the caller, for interface pointers transmitted by parameters with this function, the release must be called before another interface pointer value is assigned to it, and before the function returns, call addref for the interface pointer saved in the input parameter.

(4)Local variable rules. For partial replication interface pointers, because they only exist during the life cycle of the function, you do not need to call addref and release.

(5)Global variable rules. For interface pointers stored in global variables, you must call addref before passing them to another function. Interface pointers stored in member variables should also be processed in this way. Because any member function in the class can change the status of the interface pointer in the class.

(6)Rules for uncertain conditions. Addref and release should be called in any uncertain circumstances.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.