# Include <atlconv. h> void w2t _ (wchar * lpw) {uses_conversion; // do not forget this char * tstr = w2t (lpw) when calling the x2x macro of ATL ); // to do somethiong} // convert tchar to bstrtchar * tstr = "BSTR string. the string is released. Otherwise, the memory will kill you! "; BSTR * BSTR = t2bstr (tstr); sysfreestring (BSTR );
Everyone may think that t2bstr, w2t, and other Macros in ATL are very convenient.
But be careful, because:
They allocate memory from the stack and call alloca internally. The function allocates the memory of the returned string from the stack, and the memory will not be released until the function is called to return.
If such macros are repeatedly called for tens of thousands of times in a loop, you will inevitably generate stack overflow! However, when the number of loops is small, it seems that everything is normal, but when the number of loops is large, it will be defeated.
The solution is to encapsulate the statements that use the macro into a function independently, and then the macro can be called infinitely.