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

发布于 2024-10-20 09:56:50 字数 2465 浏览 1 评论 0原文

我有一个奇怪的问题。 我正在使用工厂注册模式,该模式在内部引用 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

I have a strange issue.
I'm using a factory registration pattern which refers internally to a std::map.
I'm using this approach for multiple components, and registration happens at multiple points in different units of compilation (aka .cpp files). This happens at static variables initialization time (before 'main' gets called).
For some strange reason I get a segfault when I'm referring to


map[key] = value;

(basically when I register a particular pfn).
Now, if I move the registrations for that particular troublesome components in the same compilation unit, everything works.
Again, if I move back the registration of just one component away, I get a segfault.

I have to say, it's not the first time I use this pattern and it works for the same executable for other components (i.e. components spread across multiple .cpp for another factory type get registered at initialization time without issues, using basically similar source code).

what should I do?
For now I've left the registration of the components in the same .cpp (and it works flawlessly).
How can I better investigate?

I'm using g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

Below 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

|煩躁 2024-10-27 09:56:50

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

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

你如何初始化地图?

You could be coming up against the static initialization problem:

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

How are you initialising the map?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文