【发布时间】:2014-07-29 18:18:15
【问题描述】:
这个应用程序是在 VS2010 上用 C++ 在 Windows XP 上开发的。
当计算机的物理内存非常低时(并且页面文件被禁用,因为它是我们的测试用例),这行代码:
std::map<UINT, std::vector<void *>> MyMap;
导致 malloc.c 中的“堆栈溢出”错误
'return HeapAlloc(_crtheap, 0, size ? size : 1);'
MyApp.exe 中 0x7c90e8e5 处未处理的异常:0xC00000FD:堆栈溢出。
此调用是从应用程序的线程之一进行的。
如果内存不足是错误,它应该抛出bad_alloc
有人可以请教这里可能是什么原因。
编辑:
这就是实际堆栈的样子
ntdll.dll!7c90e8e5()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!7c9100d3()
MyApp.exe!_heap_alloc_base(unsigned int size=72) Line 55 C
MyApp.exe!_heap_alloc_dbg_impl(unsigned int nSize=36, int nBlockUse=1, const char * szFileName=0x00000000, int nLine=0, int * errno_tmp=0x0af3f0e4) Line 431 + 0x9 bytes C++
MyApp.exe!_nh_malloc_dbg_impl(unsigned int nSize=36, int nhFlag=0, int nBlockUse=1, const char * szFileName=0x00000000, int nLine=0, int * errno_tmp=0x0af3f0e4) Line 239 + 0x19 bytes C++
MyApp.exe!_nh_malloc_dbg(unsigned int nSize=36, int nhFlag=0, int nBlockUse=1, const char * szFileName=0x00000000, int nLine=0) Line 302 + 0x1d bytes C++
MyApp.exe!malloc(unsigned int nSize=36) Line 56 + 0x15 bytes C++
MyApp.exe!operator new(unsigned int size=36) Line 59 + 0x9 bytes C++
MyApp.exe!std::_Allocate<std::_Tree_nod<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >::_Node>(unsigned int _Count=1, std::_Tree_nod<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >::_Node * __formal=0x00000000) Line 36 + 0x15 bytes C++
MyApp.exe!std::allocator<std::_Tree_nod<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >::_Node>::allocate(unsigned int _Count=1) Line 187 + 0xb bytes C++
MyApp.exe!std::_Tree_val<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >::_Tree_val<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >(const std::less<unsigned int> & _Parg=less, std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > > _Al={...}) Line 544 + 0xd bytes C++
MyApp.exe!std::_Tree<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >::_Tree<std::_Tmap_traits<unsigned int,std::vector<void *,std::allocator<void *> >,std::less<unsigned int>,std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > >,0> >(const std::less<unsigned int> & _Parg=less, const std::allocator<std::pair<unsigned int const ,std::vector<void *,std::allocator<void *> > > > & _Al={...}) Line 699 C++
【问题讨论】:
-
一旦内存不足,堆分配开始失败,这可能会以其他方式表现出来。我猜运行时正在分配失败的堆,并且运行时不会检查失败。然后它假设来自其内部分配的返回值是好的,而实际上不是。到那时,一切都会发生。
-
@DavidHeffernan 这是有道理的,但根据问题中的信息似乎并非如此。有时堆栈溢出实际上只是堆栈溢出。我的第一个想法是堆栈大小并没有那么高,但是内存不足的情况阻止了堆栈区域的扩展。
-
@hvd 这很合理。地址空间被保留,但提交失败。我认为您可以为此添加答案。
-
@DavidHeffernan 谢谢,但我认为主要思想已经在 dbasic 的回答中,尽管措辞有点混乱,所以我投了赞成票。
标签: c++ stl stack-overflow stdmap