473,806 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ 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::IXMLDOM DocumentPtr.

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 6104
Lets say I write this line:
BSTR b = m_pdoc->Getxml ();
where m_pdoc is MSXML2::IXMLDOM DocumentPtr.

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************* *********@hotma il.com
Remove only "_nos_pam"
Aug 25 '06 #2
wow , cool.

-Ab.

"Bruno van Dooren [MVP VC++]" <br************ **********@hotm ail.comwrote
in message news:OT******** *****@TK2MSFTNG P02.phx.gbl...
Lets say I write this line:
BSTR b = m_pdoc->Getxml ();
where m_pdoc is MSXML2::IXMLDOM DocumentPtr.

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************* *********@hotma il.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::IXMLDOM DocumentPtr.
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. IXMLDOMDocument Ptr 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. IXMLDOMDocument Ptr 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.orgwr ote in message
news:45******** *************** ***@news.micros oft.com...
Hi Abubakar,
Lets say I write this line:
BSTR b = m_pdoc->Getxml ();
where m_pdoc is MSXML2::IXMLDOM DocumentPtr.
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. IXMLDOMDocument Ptr 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
5750
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 instead of LPOLESTR is that determining the length of a BSTR is much faster.
2
5074
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
2859
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 = 1; dev = alldevs;
0
1273
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. I noticed that ASP.NET's 'AS STRING' doesn't return the BSTR having char(0)'s. So i must assume ASP.NET does not destroy the BSTR + i need the real data.
5
2666
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 have been failing when performing Data Access functions. In particular we have been having problems with BSTR and _bstr_t. _bstr_t(BSTR*, 1) is the original code that the problem started with. Other findings are - 1) Converting a BSTR* to a...
37
5481
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 32 bytes, because of the fact that a pointer, is 8 bytes instead of 4 bytes. The length prefix -still- is a UINT however and not a UINT_PTR.
2
1544
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 anything consise enough (I'm not particularly interested in the history of BSTR and the differences between string types etc ...
0
1331
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; wstring str; char sz_path;
2
5861
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 1, in <module>
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.