출처: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2451129&SiteID=1

 
   

« Previous Thread
  Next Thread »
  23 Nov 2007, 2:11 PM UTC
George2
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,149
SysAllocString
Answered Question Was this post helpful ?
Reply Quote

Hello everyone,

 


For the SysAllocString API, I am wondering,

 

1. any special benefits if we use SysAllocString compared with using a simple wide character string, like L"Hello"?

 

2. If we use SysAllocString to allocate constant string, then does SysAllocString allocate the constant string on heap, stack or on the constant string pool?

 


thanks in advance,
George


   Report Abuse  
  23 Nov 2007, 4:20 PM UTC
Pintu Shukla
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,501
Re: SysAllocString
Comment Was this post helpful ?
Reply Quote
According to MSDN :-

This function allocates a new string and copies the passed string into it. This function returns null if there is insufficient memory or if a null pointer is passed in

BSTR SysAllocString(
OLECHAR FAR* sz
);

sz
[in] Null-terminated string to copy. The string must be a Unicode string in 32-bit applications, and an ANSI string in 16-bit applications.

Second thing Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying

BSTR b = L"A String";


This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is

BSTR b = :: SysAllocString(L"A String");

 



Thanx




Rupesh Shukla

   Report Abuse  
  23 Nov 2007, 4:58 PM UTC
George2
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,149
Re: SysAllocString
Comment Was this post helpful ?
Reply Quote

Thanks Pintu,

 

 

 Pintu Shukla wrote:
According to MSDN :-

This function allocates a new string and copies the passed string into it. This function returns null if there is insufficient memory or if a null pointer is passed in

BSTR SysAllocString(
OLECHAR FAR* sz
);

sz
[in] Null-terminated string to copy. The string must be a Unicode string in 32-bit applications, and an ANSI string in 16-bit applications.

Second thing Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying

BSTR b = L"A String";


This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is

BSTR b = :: SysAllocString(L"A String");

 



Thanx


 

I do not quite understand what means,

 

BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation.

 

in the MSDN page you mentioned http://msdn2.microsoft.com/en-us/library/ms221069.aspx

 

I think when we use SysAllocString to allocate some memory, then we have to *concern* to free it by SysFreeString, why in MSDN, it is mentioned that there is no concern for memory management? Please feel free to correct me if I am wrong.

 

 

have a good weekend,

George


   Report Abuse  
  23 Nov 2007, 5:42 PM UTC
Pintu Shukla
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,501
Answer Re: SysAllocString
Answer Was this post helpful ?
Reply Quote

Regarding this line "BSTRs are allocated using COM memory allocation functions"

Few point as i mention in my above post:

Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying

BSTR b = L"A String";


This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is

BSTR b = :: SysAllocString(L"A String");


Thanx





Rupesh Shukla

   Report Abuse  
  23 Nov 2007, 6:10 PM UTC
ildjarn
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 556
Re: SysAllocString
Comment Was this post helpful ?
Reply Quote

A BSTR is not a COM object, it is simply a pointer to a specially laid out wide string that is strictly allocated via SysAllocString. The meaning of "BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation" is not that the string doesn't need to be cleaned up (you must eventually free the string via SysFreeString or it will leak); it means that it is allocated on a COM heap instead of your CRT's heap, so you may return it out of your COM object to another process, and that other process may clean it up (this would not be possible if it were allocated via your process' CRT).

 

In C++ you typically want to use the _bstr_t class, which will wrap the calls to SysAllocString and SysFreeString for you in a smart-pointer.


   Report Abuse  
  23 Nov 2007, 6:54 PM UTC
Pintu Shukla
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,501
Re: SysAllocString
Comment Was this post helpful ?
Reply Quote
 ildjarn wrote:
 

In C++ you typically want to use the _bstr_t class, which will wrap the calls to SysAllocString and SysFreeString for you in a smart-pointer.



Whenever you plan to use _bstr_t in your code becareful it can create memory leak in your program have a look on this post

memory leak

Thanx



Rupesh Shukla

   Report Abuse  
  26 Nov 2007, 1:10 PM UTC
George2
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,149
Re: SysAllocString
Comment This post has a code sample within it. Was this post helpful ?
Reply Quote

Thanks Pintu,

 

 

I can fully understand your reply. But is your reply answers to my previous question (below)?

 

BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation.

 

My question is why we do not need to concern about memory allocation? We still need to keep in mind when to free (SysFreeString) it. What does the above statement mean?

 

 Pintu Shukla wrote:

Regarding this line "BSTRs are allocated using COM memory allocation functions"

Few point as i mention in my above post:

Because a BSTR allocates memory before the location it nominally points to, a whole different API is necessary for working with BSTRs. For example, one may not initialize a BSTR by saying

BSTR b = L"A String";


This will correctly initialize b to a wide character array, but the byte count is uninitialized and so b is not a valid BSTR. The proper way to initialize b is

BSTR b = :: SysAllocString(L"A String");


Thanx


 

 

regards,

George


   Report Abuse  
  26 Nov 2007, 1:24 PM UTC
George2
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,149
Re: SysAllocString
Comment Was this post helpful ?
Reply Quote

Thanks ildjarn,

 

 

Great reply!

 

 ildjarn wrote:

A BSTR is not a COM object, it is simply a pointer to a specially laid out wide string that is strictly allocated via SysAllocString. The meaning of "BSTRs are allocated using COM memory allocation functions. This allows them to be returned from methods without concern for memory allocation" is not that the string doesn't need to be cleaned up (you must eventually free the string via SysFreeString or it will leak); it means that it is allocated on a COM heap instead of your CRT's heap, so you may return it out of your COM object to another process, and that other process may clean it up (this would not be possible if it were allocated via your process' CRT).

 

In C++ you typically want to use the _bstr_t class, which will wrap the calls to SysAllocString and SysFreeString for you in a smart-pointer.

 

 

regards,

George


   Report Abuse  
  26 Nov 2007, 2:49 PM UTC
George2
These stars recognize your participation in the forums. For more information about the different levels of stars, and how points are earned, please see the FAQ.


Posts 1,149
Re: SysAllocString
Comment Was this post helpful ?
Reply Quote

Thanks Pintu,

 

 

Good learning resource, and I have saved it.

 

 Pintu Shukla wrote:

 ildjarn wrote:
 

In C++ you typically want to use the _bstr_t class, which will wrap the calls to SysAllocString and SysFreeString for you in a smart-pointer.



Whenever you plan to use _bstr_t in your code becareful it can create memory leak in your program have a look on this post

memory leak

Thanx

 

 

regards,

George


AND