new[]分配的内存大小
我正在调查崩溃,并且我有以下堆栈跟踪,
...
12 4292e2c4 73148e89 KERNELBASE!RaiseException+0x58
13 4292e2fc 73150e7c MSVCR80!_CxxThrowException+0x46 [f:\dd\vctools\crt_bld\self_x86\crt\prebuild\eh\throw.cpp @ 161]
14 4292e318 386f21ba MSVCR80!operator new+0x69 [f:\dd\vctools\crt_bld\self_x86\crt\src\new.cpp @ 63]
15 4292e32c 386f1f39 StatEngineProxy!std::allocator<myClass>::allocate+0x1a [c:\program files (x86)\microsoft visual studio 8\vc\include\xmemory @ 146]
16 4292e384 386ef7e8 myModule!std::vector<myClass,std::allocator<myClass> >::_Insert_n+0xf9 [c:\program files (x86)\microsoft visual studio 8\vc\include\vector @ 1138]
17 4292e3b0 386ec20f myModule!std::vector<myClass,std::allocator<myClass> >::insert+0x88 [c:\program files (x86)\microsoft visual studio 8\vc\include\vector @ 855]
18 4292e3dc 3872bb17 myModule!std::vector<myClass,std::allocator<myClass> >::push_back+0xaf [c:\program files (x86)\microsoft visual studio 8\vc\include\vector @ 800]
....
原因很简单:bad_alloc
。问题是我怎样才能找到 stl 试图分配多少内存。
I'm investigating a crash, and I have the stack trace below
...
12 4292e2c4 73148e89 KERNELBASE!RaiseException+0x58
13 4292e2fc 73150e7c MSVCR80!_CxxThrowException+0x46 [f:\dd\vctools\crt_bld\self_x86\crt\prebuild\eh\throw.cpp @ 161]
14 4292e318 386f21ba MSVCR80!operator new+0x69 [f:\dd\vctools\crt_bld\self_x86\crt\src\new.cpp @ 63]
15 4292e32c 386f1f39 StatEngineProxy!std::allocator<myClass>::allocate+0x1a [c:\program files (x86)\microsoft visual studio 8\vc\include\xmemory @ 146]
16 4292e384 386ef7e8 myModule!std::vector<myClass,std::allocator<myClass> >::_Insert_n+0xf9 [c:\program files (x86)\microsoft visual studio 8\vc\include\vector @ 1138]
17 4292e3b0 386ec20f myModule!std::vector<myClass,std::allocator<myClass> >::insert+0x88 [c:\program files (x86)\microsoft visual studio 8\vc\include\vector @ 855]
18 4292e3dc 3872bb17 myModule!std::vector<myClass,std::allocator<myClass> >::push_back+0xaf [c:\program files (x86)\microsoft visual studio 8\vc\include\vector @ 800]
....
cause is simple: bad_alloc
. The question is how can I find how much memory stl was trying to alloc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
try...catch
添加到该类中的所有 new[] 中,然后在catch
子句中设置详细的调试信息。Add
try...catch
to all your new[]'s in that class, and then set detailed debugging information inside thecatch
clause.您可以为 STL 提供一个自定义分配器,这样它的所有内存声明都会通过您提供的函数。
这里有一个例子:
http://www.sjbrown.co。 uk/2004/05/01/pooled-allocators-for-the-stl/
You could give STL a custom allocator, so all its memory claims go through the functions you provide.
There's an example here:
http://www.sjbrown.co.uk/2004/05/01/pooled-allocators-for-the-stl/
简单的。您可以获得 CRT 的源代码。 (可能位于 C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\new.cpp 中)。因此,您可以查看第 14 帧的源代码和变量,
您会发现第 58 行看起来像
void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
。该参数size
就是您要查找的参数。Simple. You get the sources for the CRT. (Probably in
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\new.cpp
). Therefore, you can view the source and variables for frame 14You'll find that line 58 looks like
void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
. That argumentsize
is the one you're looking for.