簡體   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