最近在vs2017下开发时,程序编译正常,但是总是报栈溢出异常;经过多次检查代码和测试,发现栈空间太小导致异常;

【发现问题前】

1、创建线程时,使用的默认栈大小

VS2017 栈溢出异常:test dword ptr [eax],eax ; probe page.

2、线程内函数调用,分配了5MB内存(栈上分配)

VS2017 栈溢出异常:test dword ptr [eax],eax ; probe page.

3、调用DownLoadFile函数时,开辟栈空间失败,内存溢出

VS2017 栈溢出异常:test dword ptr [eax],eax ; probe page.

发生异常了

VS2017 栈溢出异常:test dword ptr [eax],eax ; probe page.

【问题分析】

以前版本的VS中,创建线程时不指定栈大小,使用系统默认配置,不会出现问题;但是,vs2017中就要注意了,临时缓存声明太大时,需要在线程创建时指定一个比临时缓存大的栈空间;否则,就使用全局缓存;

【解决方法】

1、创建线程时指定一个比临时缓存大的栈空间,我这里缓存只有5MB,那可以指定一个比5MB大一点的栈空间

m_hThread = CreateThread(NULL, 1024*1024*10, DownloadThread, svrAddr, 0, NULL);

2、把临时缓存放到函数外面去,也可以解决

BYTE byFileBuff[5 * 1024 * 1024];
bool CFastDFSClient::DownLoadFile(const char* szFastDfsFile, const char* szLocalFile)
{
std::string strFastDfsFile = szFastDfsFile;
std::string strLocalFile = szLocalFile;


UINT32 nRet;
BYTE *pbyFile = NULL;
UINT32 nFileSize= 0;
//BYTE byFileBuff[5 * 1024 * 1024];

相关文章:

  • 2022-01-07
  • 2022-12-23
  • 2021-08-03
  • 2021-11-24
  • 2021-05-25
  • 2022-12-23
  • 2021-12-04
  • 2021-09-25
猜你喜欢
  • 2022-12-23
  • 2021-05-24
  • 2021-06-26
  • 2022-02-03
  • 2022-12-23
  • 2021-08-17
  • 2021-09-25
相关资源
相似解决方案