Minimum code reproducing the problem:
#include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { CComBSTR ccbTest( L"foo" ); const wchar_t * pTest = ccbTest ? ccbTest : L"null string"; return 0; }
The compiler uses a temporary CComBSTR when it wants to save a pointer to pTest . It then uses the BSTR transform, available in the CCcomBSTR class, with a temporary one and stores the pointer in pTest . Then the temporary is destroyed and I remain with the pTest pointer in pTest .
The fix is ββto draw CComBSTR :
const wchar_t * pTest = ccbTest ? static_cast<BSTR>( ccbTest ) : L"null string";
I do not understand why a correction is necessary. I thought the compiler would just try to convert to BSTR on its own. Why is it temporary?
source share