A2W,W2A等转换函数,由于使用的比较平繁,所以程序内存一直上涨,导致崩溃
2016-06-06 10:24
369 查看
在写一个程序中使用了 atlconv.h 中的 A2W,W2A等转换函数,由于使用的比较平繁,所以程序内存一直上涨,导致崩溃。
最后发现是字符转换的时候会申请的内存,直接在程序中使用 WideCharToMultiByte,MultiByteToWideChar 转换完成然后再删除就可以消除这个问题了。
在一个函数的循环体中使用A2W等字符转换宏可能引起栈溢出。
#include <atlconv.h>
void fn()
{
while(true)
{
{
USES_CONVERSION;
DoSomething(A2W("SomeString"));
}
}
}
让我们来分析以上的转换宏
#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] = '\0';
MultiByteToWideChar(acp, 0, lpa, -1, lpw, nChars);
return lpw;
}
关键的地方在 alloca 内存分配内存上。
#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 an argument to free.
问题就在这里,分配的内存是在函数的栈中分配的。而VC编译器默认的栈内存空间是2M。当在一个函数中循环调用它时就会不断的分配栈中的内存。
以上问题的解决办法:
1、自己写字符转换函数,不要偷懒
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;
}
最后发现是字符转换的时候会申请的内存,直接在程序中使用 WideCharToMultiByte,MultiByteToWideChar 转换完成然后再删除就可以消除这个问题了。
在一个函数的循环体中使用A2W等字符转换宏可能引起栈溢出。
#include <atlconv.h>
void fn()
{
while(true)
{
{
USES_CONVERSION;
DoSomething(A2W("SomeString"));
}
}
}
让我们来分析以上的转换宏
#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] = '\0';
MultiByteToWideChar(acp, 0, lpa, -1, lpw, nChars);
return lpw;
}
关键的地方在 alloca 内存分配内存上。
#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 an argument to free.
问题就在这里,分配的内存是在函数的栈中分配的。而VC编译器默认的栈内存空间是2M。当在一个函数中循环调用它时就会不断的分配栈中的内存。
以上问题的解决办法:
1、自己写字符转换函数,不要偷懒
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;
}
相关文章推荐
- ssh端口映射
- linux 每天一个命令(cd) 只需要1分钟
- 嵌入式Linux裸机开发(二)——S5PV210启动过程分析
- Arduino可穿戴开发入门教程Windows平台下安装Arduino IDE
- Guava学习笔记:EventBus
- Google和伙伴们把PROMETHEUS加入Kubernetes平台
- Oracle SCN详解
- CFileFind类的详解以及应用实例
- [FAQ04794]如何修改默认数据连接开关
- 网络七层协议 五层模型 TCP连接 HTTP连接 socket套接字
- Extjs4.1中图片数据源
- wireshark使用方法(学习笔记一)
- 数据库课程设计上机安排
- 计算出给你一个随机乱敲的一个字符串最多的一个
- 【JNI】Java与C++中文字串的传递
- Activity & Fragment Transition学习概要--使用步骤
- jeecms网站开发出现会员登录页面
- 如何使用Instruments诊断App(Swift版):起步-b
- oracle 关于sum函数
- SCN与数据恢复的关系