ATL x2x String Conversion macro [Use with caution]

Source: Internet
Author: User

# 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.