繁体   English   中英

使用operator []时std :: map segfaults

[英]std::map segfaults when using operator []

我有一个奇怪的问题。 我正在使用工厂注册模式,它在内部引用std :: map。 我正在将这种方法用于多个组件,并且在不同的编译单元(即.cpp文件)中的多个点处进行注册。 这发生在静态变量初始化时(在调用'main'之前)。 出于一些奇怪的原因,我指的是一个段错误

map[key] = value;

(基本上当我注册一个特定的pfn时)。 现在,如果我在同一个编译单元中移动特定麻烦组件的注册,一切正常。 再说一遍,如果我只撤回一个组件的注册,我会得到一个段错误。

我不得不说,这不是我第一次使用这种模式,它适用于其他组件的相同可执行文件(即,在初始化时没有问题的情况下,在另一个工厂类型的多个.cpp上传播的组件使用基本相似的源代码) 。

我该怎么办? 现在我已经将组件的注册保留在同一个.cpp中(并且它可以完美地工作)。 我怎样才能更好地调查?

我正在使用g ++(Ubuntu / Linaro 4.4.4-14ubuntu5)4.4.5

在stacktrace下面

0x00007ffff793da1a in std::_Rb_tree_decrement(std::_Rb_tree_node_base*) () from /usr/lib/libstdc++.so.6
(gdb) bt
#0  0x00007ffff793da1a in std::_Rb_tree_decrement(std::_Rb_tree_node_base*) () from /usr/lib/libstdc++.so.6
#1  0x0000000000413fe9 in std::_Rb_tree_iterator, std::allocator > const, http::servlet* (*)()> >::operator-- (
    this=0x7fffffffdff0) at /usr/include/c++/4.4/bits/stl_tree.h:199
#2  0x000000000041379d in std::_Rb_tree, std::allocator >, std::pair, std::allocator > const, http::servlet* (*)()>, std::_Select1st, std::allocator > const, http::servlet* (*)()> >, std::less, std::allocator > >, std::allocator, std::allocator > const, http::servlet* (*)()> > >::_M_insert_unique (this=0x632a20, __v=...) at /usr/include/c++/4.4/bits/stl_tree.h:1179
#3  0x00000000004125f2 in std::_Rb_tree, std::allocator >, std::pair, std::allocator > const, http::servlet* (*)()>, std::_Select1st, std::allocator > const, http::servlet* (*)()> >, std::less, std::allocator > >, std::allocator, std::allocator > const, http::servlet* (*)()> > >::_M_insert_unique_ (this=0x632a20, __position=..., __v=...) at /usr/include/c++/4.4/bits/stl_tree.h:1217
#4  0x00000000004116c4 in std::map, std::allocator >, http::servlet* (*)(), std::less, std::allocator > >, std::allocator, std::allocator > const, http::servlet* (*)()> > >::insert (
    this=0x632a20, __position=..., __x=...) at /usr/include/c++/4.4/bits/stl_map.h:540
#5  0x0000000000410a63 in std::map, std::allocator >, http::servlet* (*)(), std::less, std::allocator > >, std::allocator, std::allocator > const, http::servlet* (*)()> > >::operator[] (
    this=0x632a20, __k=...) at /usr/include/c++/4.4/bits/stl_map.h:450
#6  0x000000000040e3b1 in http::servlet::add_creator (type=0x4234d0 "/gpu/european_opt_xyz", func=0x421567 )
    at ./src/http.cpp:336

您可能遇到静态初始化问题:

https://isocpp.org/wiki/faq/ctors#static-init-order

你是如何初始化地图的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM