Discussion:
Freeing Memory for a BSTR*
(too old to reply)
Marcus de Leon
2009-10-06 12:18:42 UTC
Permalink
Hi,

Here is my situation. I have a BSTR* bsNameVar that is allocated
using ::SysAllocString(), then bsNameVar gets assigned to a member
variable CString mcsNameVar. The problem is, when I use ::SysFreeString
(*bsNameVar), mcsNameVar also loses it's memory allocation. I thought
that is used a separate memory space, but I guess not. How can I free
the memory of for mcsNameVar on destruction if I don't release the
memory on bsNameVar?

Thanks,

Marcus
Igor Tandetnik
2009-10-06 12:33:50 UTC
Permalink
Post by Marcus de Leon
Here is my situation. I have a BSTR* bsNameVar that is allocated
using ::SysAllocString()
SysAllocString returns a BSTR, not a BSTR*. How does BSTR* fit into the
picture? Show some code.
Post by Marcus de Leon
then bsNameVar gets assigned to a member
variable CString mcsNameVar. The problem is, when I use
::SysFreeString (*bsNameVar), mcsNameVar also loses it's memory
allocation.
I find this hard to believe. You are doing something wrong. Again, show
the code.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Marcus de Leon
2009-10-06 12:47:52 UTC
Permalink
Post by Igor Tandetnik
Post by Marcus de Leon
Here is my situation. I have a BSTR* bsNameVar that is allocated
using ::SysAllocString()
SysAllocString returns a BSTR, not a BSTR*. How does BSTR* fit into the
picture? Show some code.
Post by Marcus de Leon
then bsNameVar gets assigned to a member
variable CString mcsNameVar. The problem is, when I use
::SysFreeString (*bsNameVar), mcsNameVar also loses it's memory
allocation.
I find this hard to believe. You are doing something wrong. Again, show
the code.
--
With best wishes,
    Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Hi, here is the code. When I reference m_csName after SysFreeString, I
get an invalid memory error.

////////////////////////////////////////////////////////////////////////////////////
CComBSTR bstrName;

pCOMPobj->GetName(&bstrName); // See below

m_csName = bstrName;

::SysFreeString(* bstrName);
bstrName = NULL;
////////////////////////////////////////////////////////////////////////////////////

// GetName(BSTR*);
////////////////////////////////////////////////////////////////////////////////////
STDMETHODIMP GetName(BSTR* pbsName)
{
CString csName = *pbsName;
csName = GetName();
*pbsPatientName = csName.AllocSysString();
}

////////////////////////////////////////////////////////////////////////////////////
Igor Tandetnik
2009-10-06 13:10:57 UTC
Permalink
Post by Marcus de Leon
CComBSTR bstrName;
pCOMPobj->GetName(&bstrName); // See below
m_csName = bstrName;
Post by Marcus de Leon
SysFreeString(* bstrName);
bstrName = NULL;
You are corrupting memory by performing double destruction. CComBSTR
frees the BSTR it wraps (in the destructor or, as in this case, when a
different string or NULL is assigned to it). Drop SysFreeString call.
Post by Marcus de Leon
STDMETHODIMP GetName(BSTR* pbsName)
{
CString csName = *pbsName;
What's the point of this line, when you reassign csName in the very next
line? In any case, since pbsName is an [out] parameter, you have to
assume that *pbsName is a random uninitialized garbage on input.
Post by Marcus de Leon
csName = GetName();
*pbsPatientName = csName.AllocSysString();
What's pbsPatientName? Is it in any way related to pbsName?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Marcus de Leon
2009-10-06 13:27:34 UTC
Permalink
Post by Igor Tandetnik
Post by Marcus de Leon
CComBSTR bstrName;
pCOMPobj->GetName(&bstrName); // See below
m_csName = bstrName;
Post by Marcus de Leon
SysFreeString(* bstrName);
bstrName = NULL;
You are corrupting memory by performing double destruction. CComBSTR
frees the BSTR it wraps (in the destructor or, as in this case, when a
different string or NULL is assigned to it). Drop SysFreeString call.
Post by Marcus de Leon
STDMETHODIMP GetName(BSTR* pbsName)
{
    CString csName = *pbsName;
What's the point of this line, when you reassign csName in the very next
line? In any case, since pbsName is an [out] parameter, you have to
assume that *pbsName is a random uninitialized garbage on input.
Post by Marcus de Leon
    csName = GetName();
    *pbsPatientName = csName.AllocSysString();
What's pbsPatientName? Is it in any way related to pbsName?
--
With best wishes,
    Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Yes pbsPatientName is pbsName.. I just renamed those and missed that
one. But your advice is good. I didn't realize CComBSTR free'd itself.

Thanks!
David Wilkinson
2009-10-06 14:04:24 UTC
Permalink
Post by Marcus de Leon
Yes pbsPatientName is pbsName.. I just renamed those and missed that
one. But your advice is good. I didn't realize CComBSTR free'd itself.
While Igor is (as always) correct, I am surprised that double destruction of a
BSTR would corrupt your CString.

Is your code working now?
--
David Wilkinson
Visual C++ MVP
Loading...