?
Solved

Do I need to free BSTR variable?

Posted on 2007-11-27
6
Medium Priority
?
1,468 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20360081
>>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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360092
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."
0
 

Author Comment

by:lapucca
ID: 20360372
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:lapucca
ID: 20360503
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20363459
Sorry, that somehow ended up in  my spam box - actually you should free the returned value, regardless of what that tool says.
0
 
LVL 21

Expert Comment

by:mastoo
ID: 20366023
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question