473,473 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Do I have to delete/release a BSTR

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.
Aug 25 '06 #1
4 6084
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
br**********************@hotmail.com
Remove only "_nos_pam"
Aug 25 '06 #2
wow , cool.

-Ab.

"Bruno van Dooren [MVP VC++]" <br**********************@hotmail.comwrote
in message news:OT*************@TK2MSFTNGP02.phx.gbl...
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
br**********************@hotmail.com
Remove only "_nos_pam"


Aug 25 '06 #3
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äsman
Aug 26 '06 #4
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.

"Kim Gräsman" <ki*@mvps.orgwrote in message
news:45**************************@news.microsoft.c om...
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äsman


Aug 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris | last post by:
I am not sure if this is the right newsgroup. But does anyone know what is the difference between a BSTR and a LPOLESTR? The only thing I could find out is that the advantage of taking BSTR...
2
by: banski | last post by:
Hi, Im trying to find out how to convert a SYSTEMTIME to BSTR. Cant find out how to do that. Hopefully some of you could help me out. Best regards Thomas
3
by: Volker Kugler | last post by:
Hi, i want to transmit a string from a C++-COM-DLL to C#. I wrote this code: STDMETHODIMP CWinpcap::hrGetAdapterNameFromAdapterNumber(int iAdapterNumber, BSTR * AdapterName) { int index =...
0
by: Edwin Knoppert | last post by:
I'm currently using a wrapper which converts an ANSI BSTR from a dll. (Yes, singlebyte but BSTR ! ) This works fine and i'm aware BSTR's returned must be destroyed by the caller, which i do. ...
5
by: bluter | last post by:
We have server components which were created by a third party and compiled in VC++5 (sp3). They run fine on NT4 and 2000, however during testing of our migration to Server 2003, these components...
37
by: Egbert Nierop \(MVP for IIS\) | last post by:
In win32 mode, a BSTR was UINT length prefixed and terminated exactly by a zero char. So if you allocated "Hello World" that would allocate 28 bytes. In x64 and (IA64 as well) it would become...
2
by: Lucy Ludmiller | last post by:
How can I write a function like this: BSTR Greeting(BSTR name) { //return "Good Morning : " + name ; } In short I'm looking for a quick tutorial on using BSTR - Google is not bringing up...
0
by: SantaClaus | last post by:
Hi all, I'm using a BSTR as an out parameter in a function which reads a file. Here is my code. HRESULT __stdcall CA::getFile(BSTR bstr,BSTR *fileCont) { if (!fileCont) return E_POINTER;...
2
by: mzdude | last post by:
I need to interface with a windows DLL that has the following signature extern "C" void Foo( BSTR in, BSTR *out ) Code so far Traceback (most recent call last): File "<pyshell#14>", line...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.