A late response that may not apply to earlier (or later) versions of Visual Studio; However, VS 12.0 has a built-in implementation of _bstr_t , and obviously, an internal instance of Data_t is created with m_RefCount of 1 when GetBSTR() called in virgin _bstr_t . So the _bstr_t life cycle in your first example looks fine:
_bstr_t description; errorInfo->GetDescription( &description.GetBSTR() );
But if _bstr_t dirty, the existing m_wstr internal pointer will be overwritten, which will leak the previous memory to which it refers.
Using the following operator& , dirty _bstr_t can be used if it is first cleared using Assign(nullptr) . Overloading also provides the convenience of using an address operator instead of GetBSTR() ;
BSTR *operator&(_bstr_t &b) { b.Assign(nullptr); return &b.GetBSTR(); }
So your first example might look like this:
_bstr_t description(L"naughty"); errorInfo->GetDescription(&description);
This rating was based on comutil.h from VS 12.0.
bvj
source share