Clause 3: select a smart pointer based on functions and implementation principles
There are many types of smart pointers. The implementation principles can be divided into two categories: ownership-based transfer and reference-based counting. The built-in std: auto_ptr in C ++ is a smart pointer based on ownership transfer. Shared_ptr In the Boost library is a smart pointer based on the reference technology (currently it has become part of C ++ 0x [6 ).
For example, the "ownership-based transfer" smart pointer will empty itself after assigning itself to other smart pointers or common pointers. In this way, the permissions of resource objects are transferred out, so that only one owner of each resource holds the access permissions at a time. For example, std: auto_ptr is a pointer like this:
View plaincopy to clipboardprint? Auto_ptr <int> ap1 (new int (0 ));
Auto_ptr <int> ap2 = ap1;
Cout <* ap1; // error. At this time, ap1 has only one null pointer in the hand.
Auto_ptr <int> ap1 (new int (0 ));
Auto_ptr <int> ap2 = ap1;
Cout <* ap1; // error. At this time, ap1 has only one null pointer in the hand.
This smart pointer is simple and can be used to detect errors as early as possible (unlike the issue that is difficult to find after the reference count error .) The unique ownership of resources by smart pointers makes it clear when to release resources.
The disadvantage is that resource sharing is inconvenient. In addition, such pointers cannot be placed in standard class templates such as STL [7 ]. Because STL parameters are passed by value instead of reference. Imagine how bad it would be if you put a smart pointer in the container and then accessed the pointer through the container. The pointer in the container points to NULL.
The "reference count"-based smart pointer maintains a reference count inside the smart pointer. When a copy of a smart pointer is generated (for example, a smart pointer is assigned to another smart pointer), the reference count increases progressively once. If a smart pointer goes out of the stack, the Destructor will reduce the reference count once. If the reference count is 0, no smart pointer is directed to this resource object. In this case, the smart pointer will destroy the resource.
The smart pointers of COM are implemented in this way, because the reference count is used by COM, and the demand also requires that COM can be accessed by multiple pointers. These pointers should have the ownership of the COM component. As follows:
View plaincopy to clipboardprint? CComPtr <IX> spIX = NULL;
HRESULT hr = spIX. CoCreateInstance (CLSID_MYCOMPONENT );
If (SUCCEEDED (hr ))
{
SpIX-> Fx ();
CComPtr <IX> spIX2 = spIX; // The Value assignment operation increases the reference count.
SpIX2-> Fx ();
SpIX-> Fx (); // After the value assignment operation, the original smart pointer can still be used.
} // When the smart pointer structure is used, it will decrease the reference count and decide whether to destroy the resources it owns.
CComPtr <IX> spIX = NULL;
HRESULT hr = spIX. CoCreateInstance (CLSID_MYCOMPONENT );
If (SUCCEEDED (hr ))
{
SpIX-> Fx ();
CComPtr <IX> spIX2 = spIX; // The Value assignment operation increases the reference count.
SpIX2-> Fx ();
SpIX-> Fx (); // After the value assignment operation, the original smart pointer can still be used.
} // When the smart pointer structure is used, it will decrease the reference count and decide whether to destroy the resources it owns.
By function, smart pointers can be divided into different categories by using "COM smart Pointers", "Memory Management smart Pointers", and "I/O smart Pointers.
For example, although shared_ptr and CComPtr are implemented based on the reference technology. However, different functions are implemented. For example, when shard_ptr finds that the reference count is 0, the memory indicated by the pointer is deleted. CComPtr calls the Release () function of the COM interface in the analysis. Both reference counting and COM component destruction are completed by the component itself. With this understanding, you may not easily use a smart pointer on a COM interface.
The standard focuses on the COM Technology and Its smart pointer, so most of the content in this article will only use CComPtr and _ com_ptr_t for example. Other types of smart pointers are not described in more detail. Interested readers can refer to other materials to learn by themselves.
Let's take a look at the overview and general methods of the two COM pointers.
Author's "liuchang5 column"