A late answer that may not apply to earlier (or later) versions of Visual Studio; however,
VS 12.0 has the _bstr_t
implementation inline, and evidently an internal Data_t
instance is created with a m_RefCount
of 1 when calling GetBSTR()
on a virgin _bstr_t
. So the _bstr_t
lifecycle in your first example looks to be okay:
_bstr_t description;
errorInfo->GetDescription( &description.GetBSTR() );
But if _bstr_t
is dirty, the existing internal m_wstr
pointer will be overwritten, leaking the previous memory it referenced.
By using the following operator&
, a dirty _bstr_t
can be used given that it's first cleared via Assign(nullptr)
. The overload also provides the convenience of utilizing the address operator instead of GetBSTR()
;
BSTR *operator&(_bstr_t &b) {
b.Assign(nullptr);
return &b.GetBSTR();
}
So, your first example could instead look like the following:
_bstr_t description(L"naughty");
errorInfo->GetDescription(&description);
This evaluation was based on comutil.h
from VS 12.0.