Do I have to delete/release a BSTR

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi,

Lets say I write this line:
BSTR b = m_pdoc->Getxml ();
where m_pdoc is MSXML2::IXMLDOMDocumentPtr.

Now "b" contains the xml text. When I exit the function in which this line
is written, do I have to take care about deleting "b"? And how do I do it ?

Regards,

-Ab.
 
Lets say I write this line:
BSTR b = m_pdoc->Getxml ();
where m_pdoc is MSXML2::IXMLDOMDocumentPtr.

Now "b" contains the xml text. When I exit the function in which this line
is written, do I have to take care about deleting "b"? And how do I do it
?

Hi,
You can use use SysFreeString for that.
Or you could use _bstr_t instead of BSTR.
It is a wrapper around BSTR, and it will do those things for you

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
wow , cool.

-Ab.

Bruno van Dooren said:
Hi,
You can use use SysFreeString for that.
Or you could use _bstr_t instead of BSTR.
It is a wrapper around BSTR, and it will do those things for you

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Hi Abubakar,
Lets say I write this line:
BSTR b = m_pdoc->Getxml ();
where m_pdoc is MSXML2::IXMLDOMDocumentPtr.
Now "b" contains the xml text. When I exit the function in which this
line is written, do I have to take care about deleting "b"? And how do
I do it ?

Actually, I believe your code is incorrect. IXMLDOMDocumentPtr is a compiler-generated
wrapper for the raw COM interface, and does not return a BSTR, but a _bstr_t.

By assigning the return value to a BSTR, you are invoking a conversion operator
on the returned _bstr_t, which is subsequently destroyed. Your b variable
is essentially pointing to random memory.

If you are going to use the wrappers generated by #import, prefer the wrapper
types to the raw automation types, and do something like this:

_bstr_t b = m_pdoc->Getxml();
// b can now be used safely until it goes out of scope

_bstr_t deallocates the contained string in its destructor, so you generally
don't need to worry about any deallocation.
 
Actually, I believe your code is incorrect. IXMLDOMDocumentPtr is a
compiler-generated

you are right but in my case it was working just fine.
If you are going to use the wrappers generated by #import, prefer the wrapper
types to the raw automation types, and do something like this:

_bstr_t b = m_pdoc->Getxml();
// b can now be used safely until it goes out of scope

_bstr_t deallocates the contained string in its destructor, so you generally
don't need to worry about any deallocation.

Understood.

Thanks.

-ab.
 
Back
Top