« links for 2005-09-20 | 回到主頁面 | links for 2005-09-23 »

September 22, 2005

Becareful to "USES_CONVERSION"


將此網頁加入【百度收藏】... 加入此網頁到【del.icio.us 書籤】 technorati

MSDN Home

BSTR bstrMyBeaster = SysAllocString (L"Tring, Tring!");
WCHAR* pwszMyWCharString = L"Tring, Tring!";

USES_CONVERSION;
LPSTR pszCharStringFromBSTR = OLE2A (bstrMyBeaster);
LPSTR pszCharStringFromLPWSTR = W2A (pwszMyWCharString);
// ...
SysFreeString (bstrMyBeaster);

I think "USES_CONVERSION;" is very convenient and easy to convert between CComBSTR and Char. But this article also tell us it may happen "Stack overflow" when your source BSTR is over 1MB. The root cause is because use  "_alloca".

I think to avoid stack overflow like this,  just use static conversion  not "USE_CONVERSION;".



VC6相關問題

由 Evan 發表於 September 22, 2005 將此網頁加入【百度收藏】... 加入此網頁到【del.icio.us 書籤】 technorati
引用
本文的引用網址:


以下是前來引用的連結 'Becareful to "USES_CONVERSION"' 來自 Blog E
迴響

Hi Evan,

Thank you for reading my article.

Just dropped in to let you know that stack overflows caused by USES_CONVERSION macros are not only applicable when the string is over 1 MB, but, even for smaller strings when used in loops - as the memory allocated on the stack accumulates loop-after-loop, and finally flows over.

Avoid this by using WideCharToMultiByte.

An easy option available to those developing on VC++ 7.0 is conversion class CW2A.

Sample to using the same is also in my FAQ:
http://www.codeguru.com/forum/showthread.php?t=337247

Best Regards,
Siddhartha

====
Siddhartha Rao
Microsoft MVP - Visual C++
Codeguru Moderator

由 Siddhartha Rao 發表於 October 13, 2005 03:44 AM
發表迴響









記住我的資訊?




(請輸入以下的驗證碼)