Solved

Do I need to free BSTR variable?

Posted on 2007-11-27
6
1,447 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
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!

 

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

Suggested Solutions

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

756 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