Introduction
Hello, ninjas. Today we are going to study object safety classes. Earlier, we studied classes and objects in C++, and now we will be learning about this new concept known as object safety classes along with other concepts. An object is marked as safe to initialize or for scripting accordingly. We will be learning other concepts too related to this topic. Let's get started.

Memory Management Class
Usually, In c++, we manage memory by allocating new and deleting the allocated memory. We call it dynamic memory allocation. In this, we will learn about these classes and how they manage memory in c++.
In general, these classes, such as CComPtr and CComPtrBase, provide support for heap pointers, smart pointers, and other memory management requirements.
Smart pointers
We know about the 'delete' and 'new' keywords used in our C codes. New is used to allocate a new memory in a heap, and delete is used to delete or free up the memory created in a heap.
Smart pointers are a way to automate this process, which means when we call a new memory allocation, we don't have to call for the delete because smart pointers do it automatically. They are a wrapper around actual raw pointers.
CComPtr Class
CComPtr class is a smart pointer class for managing COM interface pointers. This class manages the COM interface pointers. What are COM interface pointers? Understanding first what COM is.
What is COM
The full form of COM is the Component Object Model (COM). It is a binary-interface standard for software components.It allows inter-process communication object creation in a large range of programming languages. Hence, it is used in c++ also. Microsoft introduced COM in 1993.
What is COM Interface
Com Class implements a predefined group of related functions and doesn't need to represent all functions that offer a particular class. This is known as the COM interface.
The interface is the nearest equal to a pure virtual class; it contains only pure virtual methods and no other members. By convention, interface names start with "I."
Here is an example of an interface:
interface IDriveable
{
void Drive();
}
The IDriveable interface defines the operations that any driveable object must support. In this example, the IDriveable interface defines a single operation: Drive.
CComPtr Class - It is a smart pointer class for managing COM interface pointers. It is derived from CComPtrBase class and does automatic reference counting. It helps in preventing memory leakage by doing reference counting.
Constructor
CComPtr() throw ();
Assignment Operator
CComPtr::operator =
Implementation
// Making a function which has return type success or failure of the function
HRESULT CheckComBug()
{
// Declaration of the variable of HRESULT which return success or error
HRESULT bg;
CComBSTR bstrInformation;
CComBSTR bstrSource;
CComBSTR bstrHelp;
// Making use of CComPtr class, IBugInfo is the interface
CComPtr<IBugInfo> pBugInfo;
// Assigning value of the method GetErrorInfo to bg
bg = ::GetErrorInfo(0, &pBugInfo);
// Checking if bg is success or failure S_OK = success
if (bg != S_OK)
return bg;
// Assigning value of the method GetDescription to bg
bg = pBugInfo->GetDescription(&bstrInformation);
if (FAILED(bg))
{
// If failed then decrease the reference count for an interface of an object.
pBugInfo->Release();
return bg;
}
// Assigning the value of the method GetSource to bg
bg = pBugInfo->GetSource(&bstrSource);
if (FAILED(bg))
{
// If failed, then decrease the reference count for an interface of an object.
pBugInfo->Release();
Return bg;
}
// Assigning a value of the method GetHelpFile to bg
bg = pBugInfo->GetHelpFile(&bstrHelp);
if (FAILED(bg))
{
// If failed, then decrease the reference count for an interface of an object.
pBugInfo->Release();
return bg;
}
pBugInfo->Release();
// Returning the overall function type
return S_OK;
}
CComPtrBase Class
This class serves as the basis for classes that use smart pointers and COM-based memory routines. Other smart pointers that make use of COM memory management are built on the basis of this class. The derived classes also have new constructors and operators of their own.
Public Methods
This has various public methods.
| Name | Description |
| CComPtrBase::Advise | This method creates a connection between the CComPtrBase connection point and a client's sink. |
| CComPtrBase::Attach | This method takes ownership of an existing pointer. |
| CComPtrBase::CoCreateIinstance | This method creates an object of the class associated with a specified Class ID or Program ID. |
| CComPtrBase::CopyTo | This method copies the CComPtrBase pointer to another pointer variable. |
| CComPtrBase::Detach | This method releases the right of the pointer. |
| CComPtrBase::IsEqualObject | This method checks if the specified IUnknown points to the same object associated with the CComPtrBase object. |
Public Operators
This has various public operators.
| Name | Description |
| CComPtrBase::operator T* | The cast operator. |
| CComPtrBase::operator ! | The NOT operator. |
| CComPtrBase::operator & | The address-of & operator. |
| CComPtrBase::operator * | The pointer-to * operator. |
| CComPtrBase::operator < | The less-than operator. |
| CComPtrBase::operator == | The equality operator. |
| CComPtrBase::operator -> | The pointer-to-members operator. |





