zoukankan      html  css  js  c++  java
  • 谨慎使用A2W等字符转换宏

    在一个函数的循环体中使用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] = '';
       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;
    }
    等等一个一个的实现。
    
    2、把字符转换部分放到一个函数中处理。
    
    void fn2()
    {
        USES_CONVERSION;
        DoSomething(A2W("SomeString"));
    }
    
    void fn()
    {
        while(true)
        {
            fn2();
        }
    }

  • 相关阅读:
    Java高并发24-使用自定义锁生成一个消费模型
    Java高并发连载23-基于AQS实现自定义同步器
    JavaScript连载38-编写评论界面
    Java高并发22-AQS条件变量的支持
    Java高并发21-AQS在共享,独占场景下的源码介绍
    SSH 集成Shiro和EhCache,设置登录超时时间无效解决办法。
    Vue3.0 + Echarts 实现地区人口数量分布展示
    从零开始学VUE之网络模块(Axios)
    从零开始学VUE之VueX(modules)
    从零开始学VUE之VueX(actions)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318750.html
Copyright © 2011-2022 走看看