Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

Do I have to delete/release a BSTR

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Abubakar - 25 Aug 2006 07:52 GMT
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.
Bruno van Dooren [MVP VC++] - 25 Aug 2006 09:53 GMT
> Lets say I write this line:
> BSTR b = m_pdoc->Getxml ();
[quoted text clipped - 3 lines]
> 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

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Abubakar - 25 Aug 2006 13:48 GMT
wow , cool.

-Ab.

> > Lets say I write this line:
> > BSTR b = m_pdoc->Getxml ();
[quoted text clipped - 15 lines]
>     bruno_nos_pam_van_dooren@hotmail.com
>     Remove only "_nos_pam"
Kim Gräsman - 26 Aug 2006 13:42 GMT
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.

--
Best Regards,
Kim Gräsma
Abubakar - 29 Aug 2006 11:03 GMT
> 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:
[quoted text clipped - 4 lines]
> _bstr_t deallocates the contained string in its destructor, so you generally
> don't need to worry about any deallocation.

Understood.

Thanks.

-ab.

> Hi Abubakar,
>
[quoted text clipped - 24 lines]
> Best Regards,
> Kim Gräsman

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.