c++ - 使用运算符 [] 时的 std::map 段错误

标签 c++ initialization segmentation-fault factory stdmap

我有一个奇怪的问题。 我正在使用内部引用 std::map 的工厂注册模式。 我将这种方法用于多个组件,并且注册发生在不同编译单元(也称为 .cpp 文件)的多个点。这发生在静态变量初始化时(在调用“main”之前)。 出于某种奇怪的原因,当我指的是

映射[键] = 值;

(基本上当我注册一个特定的 pfn 时)。 现在,如果我在同一编译单元中移动该特定麻烦组件的注册,一切正常。 同样,如果我将一个组件的注册移回原处,则会出现段错误。

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

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

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

在堆栈跟踪之下

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

你如何初始化 map ?

关于c++ - 使用运算符 [] 时的 std::map 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219167/

相关文章:

c++ - 转发引用是否仍然是右值引用?

c++ - 让 VS10 调试器跳转某些库有问题吗?

c++ - 在 C++ 中测量函数的执行时间

c++ - 使用 _mm_load_pd 时函数崩溃

c++ - 不在 C++ 程序中使用 libpq 检索 NOTIFY

Java错误的值初始化

c++ - C++ 中的结构与数组

用于模块初始化的 Python 钩子(Hook)

iphone - iphone 无法解释的段错误

c++ - boost::weak_ptr<T>.lock() 因 SIGSEGV 段错误而崩溃