Solved

Do I need to free BSTR variable?

Posted on 2007-11-27
6
1,451 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

733 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