Solved

Do I need to free BSTR variable?

Posted on 2007-11-27
6
1,414 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
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:lapucca
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now