C++ 11 unordered_map 分段错误

Posted

技术标签:

【中文标题】C++ 11 unordered_map 分段错误【英文标题】:C++ 11 unordered_map segmentation fault 【发布时间】:2013-02-04 17:11:26 【问题描述】:

因此,当我使用多个线程运行此程序时,我不断遇到分段错误:

class InvertedIndex 
private:
    unordered_map<string, Details> index;
    mutex indexInsert;

public:
  void addTerms(pair<string, list<string>> parsedReport) 
    unordered_map<string, Details>::iterator indexIterator;

        for (auto term = parsedReport.second.begin(); term != parsedReport.second.end(); ++term) 
            indexInsert.lock();
            indexIterator = index.find(*term);

            if (indexIterator == index.end()) 
                Details details;
                details.addOccurrence(parsedReport.first);
                index.insert(pair <string, Details> (*term, details));
             else
                indexIterator->second.addOccurrence(parsedReport.first);

            indexInsert.unlock();
        
    

当我使用 GDB 调试程序时,它告诉我问题出在 index.find(*term) 上,这与哈希有关。当我用一个我觉得奇怪的线程运行它时它运行良好,因为我在它周围有锁。


编辑: 这是所要求的堆栈跟踪:

Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff75de700 (LWP 3842)]
[New Thread 0x7ffff6ddd700 (LWP 3843)]
[New Thread 0x7ffff65dc700 (LWP 3844)]
[New Thread 0x7ffff5ddb700 (LWP 3845)]
[New Thread 0x7ffff55da700 (LWP 3846)]
[New Thread 0x7ffff4dd9700 (LWP 3847)]
[New Thread 0x7ffff45d8700 (LWP 3848)]
[New Thread 0x7ffff3dd7700 (LWP 3849)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff65dc700 (LWP 3844)]
0x000000000041c8bf in std::__detail::_Hash_code_base<std::string, std::pair<std::string const, Details>, std::_Select1st<std::pair<std::string const, Details> >, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::_M_bucket_index (this=0x7fffffffde10, __p=0xc0, __n=467) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable_policy.h:793
793        return _M_h2()(__p->_M_hash_code, __n); 
(gdb) bt
#0  0x000000000041c8bf in std::__detail::_Hash_code_base<std::string, std::pair<std::string const, Details>, std::_Select1st<std::pair<std::string const, Details> >, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::_M_bucket_index (this=0x7fffffffde10, __p=0xc0, __n=467)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable_policy.h:793
#1  0x0000000000419591 in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_M_bucket_index (
    this=0x7fffffffde10, __n=0xc0) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:461
#2  0x000000000041561c in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_M_find_before_node (
    this=0x7fffffffde10, __n=57, __k="bypass", __code=1325113244381254371) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:1081
#3  0x0000000000410672 in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_M_find_node (
    this=0x7fffffffde10, __bkt=57, __key="bypass", __c=1325113244381254371) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:478
#4  0x000000000040c9e6 in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::find (this=0x7fffffffde10, __k=
    "bypass") at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:939
#5  0x0000000000408e50 in InvertedIndex::addTerms (this=0x7fffffffdd00, parsedReport=...) at InvertedIndex.hpp:83
#6  0x00000000004091f2 in InvertedIndex::addReport (this=0x7fffffffdd00, fileName="/home/brodie/workspace/inverted_index/resources/test_reports/report6.xml") at InvertedIndex.hpp:104
#7  0x0000000000409e26 in InvertedIndex::loadIndex()::lambda()#1::operator()() const (__closure=0x6448e0) at InvertedIndex.hpp:169
#8  0x0000000000425128 in std::_Function_handler<void (), InvertedIndex::loadIndex()::lambda()#1>::_M_invoke(std::_Any_data const&) (__functor=...)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1926
#9  0x0000000000426ba4 in std::function<void ()>::operator()() const (this=0x6448b0) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:2311
#10 0x0000000000426df1 in std::__invoke<std::function<void ()>>(std::function<void ()>&) (__f=...) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:235
#11 0x0000000000426dc6 in std::reference_wrapper<std::function<void ()> >::operator()<>() const (this=0x7ffff65dbcc0) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:468
#12 0x0000000000426cc2 in std::_Bind_simple<std::reference_wrapper<std::function<void ()> > ()>::_M_invoke<>(std::_Index_tuple<>) (this=0x7ffff65dbcc0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1598
#13 0x0000000000426a3b in std::_Bind_simple<std::reference_wrapper<std::function<void ()> > ()>::operator()() (this=0x7ffff65dbcc0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1586
#14 0x0000000000426717 in std::_Function_handler<void (), std::reference_wrapper<std::_Bind_simple<std::reference_wrapper<std::function<void ()> > ()> > >::_M_invoke(std::_Any_data const&) (__functor=...)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1956
#15 0x0000000000426ba4 in std::function<void ()>::operator()() const (this=0x7fffd40008c8) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:2311
#16 0x000000000042681e in std::__future_base::_Task_setter<std::unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter>, void>::operator() (this=0x7fffd40008c0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:1236
#17 0x0000000000426587 in std::_Function_handler<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> (), std::__future_base::_Task_setter<std::unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter>, void> >::_M_invoke(std::_Any_data const&) (__functor=...) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1912
#18 0x000000000040bde5 in std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>::operator()() const (this=0x7ffff65dbce0)
#19 0x0000000000408568 in std::__future_base::_State_base::_M_do_set(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&) (this=0x644838,
    __f=..., __set=@0x7ffff65dbc1f: false) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:473
#20 0x000000000041b403 in std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)>::operator()(std::__future_base::_State_base*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&) const (this=0x7ffff65dbba8, __object=0x644838,
    __args#0=..., __args#1=@0x7ffff65dbc1f: false) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:554
#21 0x00000000004184fb in std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)> (std::__future_base::_State_base*, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool>)>::_M_invoke<0ul, 1ul, 2ul>(std::_Index_tuple<0ul, 1ul, 2ul>) (this=0x7ffff65dbb90) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1598
#22 0x000000000041429f in std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)> (std::__future_base::_State_base*, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool>)>::operator()() (this=0x7ffff65dbb90) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1586
#23 0x000000000040fa7f in std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)> (std::__future_base::_State_base*, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool>)> >() () at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/mutex:787
#24 0x00000036e920cac0 in pthread_once () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_once.S:103
#25 0x0000000000404266 in __gthread_once (__once=0x6448a4, __func=0x403eb0 <__once_proxy@plt>) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/x86_64-redhat-linux/bits/gthr-default.h:718
#26 0x000000000040bc8d in std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&), std::__future_base::_State_base* const, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool> >(std::onc---Type <return> to continue, or q <return> to quit---
e_flag&, void (std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&), std::__future_base::_State_base* const&&, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >&&, std::reference_wrapper<bool>&&) (__once=...,
    __f=<unknown type in /home/brodie/workspace/inverted_index/src/InvertedIndexMain, CU 0x0, DIE 0x3f48a>) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/mutex:819
#27 0x0000000000408333 in std::__future_base::_State_base::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) (this=0x644838, 
    __res=..., __ignore_failure=false) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:362
#28 0x0000000000425f92 in std::__future_base::_Task_state<void ()>::_M_run() (this=0x644838) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:1271
#29 0x0000000000425c9c in std::packaged_task<void ()>::operator()() (this=0x644928) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:1379
#30 0x0000000000425ad8 in any_packaged<void>::execute (this=0x644920) at ThreadPool.hpp:27
#31 0x0000000000408743 in any_packaged_task::operator() (this=0x7ffff65dbdd0) at ThreadPool.hpp:40
#32 0x0000000000404baf in Worker::operator() (this=0x641120) at ThreadPool.hpp:109
#33 0x0000000000426068 in std::_Bind_simple<Worker ()>::_M_invoke<>(std::_Index_tuple<>) (this=0x641120) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1598
#34 0x0000000000425cb9 in std::_Bind_simple<Worker ()>::operator()() (this=0x641120) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1586
#35 0x0000000000425af6 in std::thread::_Impl<std::_Bind_simple<Worker ()> >::_M_run() (this=0x641108) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/thread:115
#36 0x00000036ec6b2a20 in std::(anonymous namespace)::execute_native_thread_routine (__p=<optimized out>) at ../../../../../libstdc++-v3/src/c++11/thread.cc:73
#37 0x00000036e9207d15 in start_thread (arg=0x7ffff65dc700) at pthread_create.c:308
#38 0x00000036e8ef246d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:114

【问题讨论】:

不是实际代码。期待同样的答案。 实际代码超过 1000 行,这就是我要解决的问题。这是访问这些变量的唯一区域。 您需要string term; 做什么?你用auto term = ...隐藏它。 你的代码在我看来没问题。我可以想到两个可能的选项 1)您的 std::string 实现了 COW 或其他一些不是线程安全的优化(不太可能)2)问题在于您没有向我们展示的代码(更有可能) 请注意,分段错误不一定源自它最终发生的位置。不过考虑从 gdb 发布实际的堆栈跟踪。 【参考方案1】:

在我看来,您的 mutex 类型实际上并没有提供互斥。您还记得使用-pthread 编译器标志吗?

【讨论】:

我其实没有,我是用 g++ -Wall -c "%f" -std=c++0x -lxerces-c -lconfig++ 编译然后添加根据您的建议进行 pthread 标志 g++ -Wall -c "%f" -std=c++0x -lxerces-c -lconfig++ -pthread 并且跟踪似乎显示了同样的问题。

以上是关于C++ 11 unordered_map 分段错误的主要内容,如果未能解决你的问题,请参考以下文章

分段错误:在 C++ 中弹出向量时出现 11

在 C++ 索引程序中使用向量时出现分段错误 11

C++ 11 Boost 1.65 recursive_directory_iterator 给出分段错误错误

C++ 标准委员会是不是打算在 C++11 中 unordered_map 破坏它插入的内容?

向量的 C++ 分段错误

C++ 的核心转储分段错误