Recently I've been looking into BSTRs, and have found how 'touchy' they can be. I saw this in a COM class in the class destructor regarding a m_strBStr member...
if(m_strBStr)
SysFreeString(m_strBStr);
When I saw it I thought, "How would the BSTR have a zero assigned to it?", because I didn't see anywhere in the COM class where that had been done. In fact, I thought it was an illegal operation on a BSTR. However, I wrote a test app to check it, and I'm not getting any crashes...
#include <windows.h>
#include <oaidl.h>
#include <stdio.h>
int main(void)
{
wchar_t* pWideCh=0;
BSTR strName=0;
wprintf(L"pWideCh = %u\n",(unsigned)pWideCh);
wprintf(L"strName = %u\n",(unsigned)strName);
strName=SysAllocString(L"Fred");
wprintf(L"strName = %s\n",strName);
SysFreeString(strName);
strName=0;
wprintf(L"strName = %u\n",(unsigned)strName);
getchar();
return 0;
}
/*
pWideCh = 0
strName = 0
strName = Fred
strName = 0
*/
Since a BSTR is just a typedef of a wchar_t*, I guess its OK. Could someone let me know for sure?