Destructors for COM components

   I used the ATL COM wizard to create a component. I made an interface IDBManager. The implementation class is CDBManger. This class handles Database Operations. In the constructor of this class I've opened a few connections to databases. I want to release these connections when the component is killed....where do i write the code for the destructor itself? Or anywhere else?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

CodealotConnect With a Mentor Commented:
To do that you can override 'FinalRelease' on the ATL class your class is deriving from. From the ATL Library Reference:

CComObjectRootEx::FinalReleaseSee Also
CComObjectRootEx Overview | Class Members | CComObjectRootEx::FinalConstruct
You can override this method in your derived class to perform any cleanup required for your object.

void FinalRelease( );
By default, CComObjectRootEx::FinalRelease does nothing.

Performing cleanup in FinalRelease is preferable to adding code to the destructor of your class since the object is still fully constructed at the point at which FinalRelease is called. This enables you to safely access the methods provided by the most derived class. This is particularly important for freeing any aggregated objects before deletion.
Destructor will do if you're only diong non-COM related stuff.
All Courses

From novice to tech pro — start learning today.