ATL has built-in support for tracing the reference count through AddRef and Release calls.
To enable reference count debugging
Add the following line before including atlbase.h:
#define _ATL_DEBUG_INTERFACES
With reference count debugging enabled, the debug output window of Visual C++ will display the current reference count for the interface along with the corresponding class name and interface name every time AddRef or Release is called.