Question

I have a COM code where i use BSTR.

In that the application crashes when using ::SysFreeString()

However the same works in WINDOWS2003, WIN7, WINXP etc.

Any idea on what is causing the problem or what could be wrong. Sample code is as below

class CFoo::IUser 
{  
BSTR UserName; 

}
HRESULT CBar::FooMethod(VARIANT *vOut) 
{
//create interface, variant and fill the object.

BSTR bstrname = ::SysAllocString(_T("Username")); 
//use bstrname to fill interface structure.
//finished using now deallocate memory

::SysFreeString(bstrname); // crashing here only in windows 2008 R2 

}
Was it helpful?

Solution

I would suggest to stop using the raw forms like BSTR. Instead use wrapper classes like _bstr_t which encapsulates the raw BSTR pointer. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate.

More info at following link

http://msdn.microsoft.com/en-us/library/zthfhkd6%28v=vs.80%29.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top