The lifetime of a COM Object)

Source: Internet
Author: User

Lifetime of processing COM objects

When an object is created, the system allocates the required memory resources. When the object is no longer needed, the object should be destroyed so that the system can use the memory resources for other purposes. In C ++, you can use new and delete to directly control the lifetime of an object. However, the COM object cannot be directly created or destroyed. The reason is that multiple applications may use the same COM object at the same time. If one of the applications revokes the COM Object, other applications that use this COM object may fail to run. The system uses reference count to control the lifetime of a COM object.

The reference count of an object is a number used to identify how many times an interface has been applied. Once the API is applied, the count is increased by 1. When the application no longer needs this interface and releases it, the reference count is reduced by 1. If the reference count is greater than 0, the COM object will continue to exist in the memory. When the reference count reaches 0, this object will be destroyed by itself, and you do not need to know anything about the reference count of this COM object. You only need to get or release an appropriate interface, and the object will determine its own life.

Note: In a com application, it is critical to properly handle the reference count, otherwise Memory leakage may easily occur. The most common mistake for com programmers is not to release an interface. When this happens, the reference count will never reach 0, so that the COM object will always exist in the memory.

Increase 1 and decrease 1 of reference count

Whenever you get a new interface pointer, the reference count will increase the reference count by calling iunkown: addref, but your application usually does not need to call this method. If you create an object to get an interface pointer or get it through iunknown: QueryInterface, the object will automatically increase the reference count. However, if you get the interface pointer through other methods, such as copying an existing pointer, you must display the call iunkonwn: addref to increase the application count. Otherwise, when you release the previous interface pointer, the COM object may be destroyed, even though you may need the copied pointer.

Whether you add reference counting to your object, you must release this interface pointer. When you no longer need this interface pointer, you should call iunknown: release to reduce the reference count value. In practice, null values are usually used to initialize pointer variables. When you release this pointer, you also need to negative the null value to this pointer. This allows you to test the interface pointer in your program's clear code segment. In this case, if the pointer value is not null, it indicates that some pointers exist. You should release them before your application ends.

The following code segment expands the sample code discussed in the article "using the con interface" to illustrate how to handle the reference count:

Idirectsoundbuffer8 * pdsbprimary = NULL;
Idirectsound3dlistener8 * pdslistener = NULL;
Idirectsound3dlistener8 * pdslistener2 = NULL;
...
// Create the object and obtain an additional interface.
// The object increaments the reference count.
If (failed (hR = g_pps-> createsoundbuffer (& dsbd, & pdsbprimary, null )))
Retrun hr;

// Make a copy of the idirectsound3dlistener8 interface prointer
// Call addref to incremnet the reference count and to ensure that
// The object is not destroyed prematurely

Pdslistener2 = pdslistener;
Pdslintener2-> addref ();
...
// Cleanup code. Check to see if the pointers are still active.
// If they are, call release to release the interface.
If (pdsbprimary! = NULL)
{
Pdsbprimary-> release ();
Pdsbprimary = NULL;
}

If (pdslistener! = NULL)
{
Pdslistener-> release ();
Pdslistener = NULL;
}
If (pdslistener2! = NULL)
{
Pdslistener2-> release ();
Pdslistener2 = NULL;
}

 

Note: Due to the limited translation level, errors are inevitable. If you find that the translation is incorrect, ask your friends on the road to correct it. Thank you.

Can be reproduced at will.

Translator: closeall

 

Translated by 2005.09.04

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.