Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

Do I need to free BSTR variable?

Hi, do I need to use SysFreeString in the following condition?
BSTR a;
_bstr_t b;
a = b.copy();
Do I need to use SysFreeString(a); when I'm done with this local variable?  I'm getting a Error in Purify tool that says I'm trying to free a freed memory in Heap memory.
Thank you.
Avatar of jkr
jkr
Flag of Germany image

>>Do I need to use SysFreeString(a); when I'm done with this local variable?

No, the destructor of '_bstr_t' will care about this, that's what this utility class is good for.
See also http://msdn2.microsoft.com/en-us/library/zthfhkd6(VS.80).aspx ("_bstr_t Class"):

"A _bstr_t object encapsulates the BSTR data type. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate. The _bstr_t class uses reference counting to avoid excessive overhead."
Avatar of lapucca
lapucca

ASKER

I just want to make sure that I understand this.  Because the BSTR variable a, is assigned the value from b.copy(), hence a doesn't need to have SysFreeString for a, because b will take care of that, right?  Because I'm not freeing _bstr_t b, I was freeing BSTR a.  Thank you.
Avatar of lapucca

ASKER

I guess my question is when _bstr_t b.copy(), does this make a new BSTR that gets assign to BSTR a variable so shouldn't a be responsible for freeing the strin using SysFreeString?  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
b takes care of itself.  The copy, referenced by "a" needs to be released with SysFreeString.  And I assume it was omitted for the sample posted but you don't have any string initialized in b.