文章1原文URL: http://untidy.net/blog/2004/12/17/uses_conversion/
文章2原文URL: http://www.codeguru.com/forum/showthread.php?p=1135929#post1135929
Yesterday my team at work finally got to the bottom of a crash in a product that was very difficult to track down. We had a thread running in a product that was now being required to run for much longer, and was eventually producing a stack overflow exception (which we’d not seen before). As is the nature of such an exception, it often came from different points in the thread depending on the work that was being done.
To cut a long and painful bug-tracking story short, it turns out that the memory allocation performed by the macros related to ATL’s USES_CONVERSION text-encoding conversion system is performed by the alloca
function. This function allocates memory on the stack (an AHA! moment) and what’s more this memory does not obey the standard stack-scoping rules:
void fn()
{
while(true)
{
{
USES_CONVERSION;
DoSomething(A2W("SomeString"));
}
}
}
Some might expect the above code to release the memory allocated by A2W each time around the loop - wrong! The memory allocated by alloca does not get released until the current function is left, so to fix this problem you have to write code like this:
void fn2()
{
USES_CONVERSION;
DoSomething(A2W("SomeString"));
}
void fn()
{
while(true)
{
fn2();
}
}
This code was part of a Visual C++ 6 project, and apparently in VC7 there are alternative better macros that use proper stack scoping for allocation - good! We found the first signs of the true location of the bug by converting the project to VC7 and running it there - where the stack trace exception always seemed to occur on the line where the A2W macro was being used. I think this was probably just good fortune rather than VC7 being exceptionally helpful (no pun intended!).
So: Don’t use USES_CONVERSION and friends in thread functions or tight loops - you have been warned!
=====================================================
ATL String: What's wrong with the USES_CONVERSION macros? How to avoid using them?
BSTR bstrMyBeaster = SysAllocString (L"Tring, Tring!");
WCHAR* pwszMyWCharString = L"Tring, Tring!";
USES_CONVERSION;
LPSTR pszCharStringFromBSTR = OLE2A (bstrMyBeaster);
LPSTR pszCharStringFromLPWSTR = W2A (pwszMyWCharString);
// ...
SysFreeString (bstrMyBeaster);
Q: If it is simple and if it works, then, what's wrong in using it?
A: Macros such as OLE2A, W2A and the likes cause Stack Overflows when used in loops.
The reason:
They allocate memory using _alloca
_alloca allocates memory on the Function Stack. This memory is released ("popped") only on function exit.
So, a loop that loops too often and converts strings can result in a situation where the stack has no space left to offer.
This situation causes a Stack Overflow Exception.
Sample of a Prospective Stack Overflow Exception causing Function:
CW2T pszString (L"Tring Tring");
// Use it as a LPCTSTR
std::cout << pszString;
For more information on how to convert strings using ATL 7.0, visit: ATL 7.0 String Conversion Classes and Macros.
转http://blog.csdn.net/vcleaner/archive/2007/08/08/1731171.aspx
Unicode情况下CString和string之间的互换
添加头文件引用
在 mfc 下使用要包含 afxconv.h
atl 下是 atlconv.h