Use Uses_conversion with caution

Source: Internet
Author: User
use uses_conversion with caution

Uses_conversion is a macro definition in ATL. Used for encoding conversions (more is CString to LPCWSTR conversion). Use to include header files under ATL # # "Atlconv.h"

Use uses_conversion be careful, they allocate memory from the stack until the function that calls it returns, and the memory is not freed. If in a loop, this macro is repeatedly called tens of thousands of times, it will inevitably produce stackoverflow.

converting macros using a2w characters in the loop body of a function can cause stack overflow.

#include <atlconv.h>
VOID Fn ()
{
while (true)
{
{
Uses_conversion;
DoSomething (a2w ("somestring"));
}
}
}

Let's analyze the conversion macros above

#define A2W (LPA) (\
((_lpa = lpa) = = NULL)? NULL: (\
_convert = (Lstrlena (_LPA) +1), \
Atla2whelper ((LPWSTR) alloca (_convert*2), _lpa, _convert))

#define Atla2whelper Atla2whelper

Inline LPWStr WINAPI atla2whelper (lpwstr lpw, LPCSTR lpa, int nchars, UINT ACP)
{
Atlassert (LPA! = NULL);
Atlassert (LPW! = NULL);
Verify that no illegal character present
Since LPW was allocated based on the size of LPA
Don ' t worry about the number of chars
Lpw[0] = ' + ';
MultiByteToWideChar (ACP, 0, LPA,-1, LPW, nchars);
return LPW;
}

The key place is on alloca memory allocation memory.
#define ALLOCA _alloca

_alloca
Allocates memory on the stack.

Remarks
_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function

Exits. Therefore, do not pass the pointer value returned by _ALLOCA as a argument to free.

The problem is that the allocated memory is allocated in the stack of functions. The VC compiler default stack memory space is 2M. The memory in the stack is allocated continuously when it is called in a function.

Solutions to the above problems:
1. Write character conversion function, don't be lazy
Function that safely converts a ' WCHAR ' String to ' LPSTR ':
char* convertlpwstrtolpstr (LPWStr lpwszstrin)
{
LPSTR pszout = NULL;
if (Lpwszstrin! = NULL)
{
int ninputstrlen = Wcslen (Lpwszstrin);

Double NULL Termination
int noutputstrlen = WideCharToMultiByte (CP_ACP, 0, Lpwszstrin, Ninputstrlen, NULL, 0, 0, 0) + 2;
pszout = new char [Noutputstrlen];

if (pszout)
{
memset (Pszout, 0x00, Noutputstrlen);
WideCharToMultiByte (CP_ACP, 0, Lpwszstrin, Ninputstrlen, Pszout, Noutputstrlen, 0, 0);
}
}
return pszout;
}
And so on one by one realization.

2, put the character conversion part into a function to deal with.

void Fn2 ()
{
Uses_conversion;
DoSomething (a2w ("somestring"));
}

VOID Fn ()
{
while (true)
{
FN2 ();
}
}

If you do not know this problem, it is difficult to find out the cause of the crash after use.

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.