When to use SysFreeString on a BSTR?
Posted on 1998-07-13
We are developing 32 bit Automation components using VB 5.0 and MFC ClassWizard. On our components we have several accessor methods that manipulate a CString and then return a BSTR back to the consumer by using the AllocSysString() method:
strResult = "Something.."
return ( strResult.AllocSysString();
The consumer would use a smart pointer to a SomeClass instance to access this function like this:
// create smart pointer
// get the string
aBSTRString = anObject->getSomeString()
// do something with it, like convert back to a CString
strResult = CString(aString);
// Free the BSTR... Is this necessary?
BoundsChecker flags an error indicating the SysFreeString is freeing memory in a bad address location.
The MSDN indicates that you should use ::SysFreeString in the rare case that you need to deallocate the returned string. Should we not be using this function on BSTR's converted from CStrings using the CString::AllocSysString method?