boost::tuple 和 std::map 的分段错误

发布于 2024-10-08 21:59:55 字数 2326 浏览 0 评论 0原文

我在使用类似于以下代码的代码时遇到问题:

std::map<boost::tuple<int, int, int>, int> m;
boost::tuple<int, int, int> key = boost::make_tuple(1,2,3);
m.find(key);

编译器没有看到任何错误。但是当我启动程序时,出现了奇怪的分段错误。所以我想找到导致它的代码行。 GDB然后告诉我:

Program received signal SIGSEGV, Segmentation fault.
0x0809f40a in boost::tuples::detail::lt<boost::tuples::cons<int,
boost::tuples::cons<int, boost::tuples::cons<int, boost::tuples::null_type> > >, 
boost::tuples::cons<int, boost::tuples::cons<int, boost::tuples::cons<int, 
boost::tuples::null_type> > > > (lhs=..., rhs=...)
at /usr/local/lib/boost_1_45_0/boost/tuple/tuple_comparison.hpp:73
73             lt(lhs.get_tail(), rhs.get_tail()));

不幸的是,到目前为止我找不到任何解决此类问题的方法。

有人看到我在这里错过了什么吗?

编辑:所以我做了一些进一步的调查。导致问题的对象是用户定义的对象。实际上,boost 的东西和地图的使用似乎都不是原因,因为矢量也会发生错误!

class A {
void foo();
private:
    std::vector<int> v;
}
void A::foo() {
    ...
    v = std::vector<int>(); // Here already comes a segfault.
    ...
}

我还尝试在单独的类中重现该错误。不幸的是我无法引发那里的错误。

现在gdb告诉我:

Program received signal SIGSEGV, Segmentation fault.
0x010cae21 in free () from /lib/libc.so.6
(gdb) backtrace 
#0  0x010cae21 in free () from /lib/libc.so.6
#1  0x00fd7441 in operator delete(void*) () from /usr/lib/libstdc++.so.6
#2  0x080668c7 in __gnu_cxx::new_allocator<int>::deallocate (this=0xbfffdb04, 
__p=0x210bf) at /usr/include/c++/4.4/ext/new_allocator.h:95
#3  0x08064b8d in std::_Vector_base<int, std::allocator<int> >::_M_deallocate 
(this=0xbfffdb04, __p=0x210bf, __n=105047440)
at /usr/include/c++/4.4/bits/stl_vector.h:146
#4  0x0806246a in std::_Vector_base<int, std::allocator<int> >::~_Vector_base
(this=0xbfffdb04, __in_chrg=<value optimized out>)
at /usr/include/c++/4.4/bits/stl_vector.h:132
#5  0x080604d4 in std::vector<int, std::allocator<int> >::~vector (this=0xbfffdb04,  
__in_chrg=<value optimized out>)
at /usr/include/c++/4.4/bits/stl_vector.h:313
#6  0x0809e151 in ModelManager::emitSignal (this=0xbffff20f, o=crossroad, r=none, 
restrID=-1, signal=add, id=-5, colStart=-1, colEnd=-1)
at .build_debug/src/model/modelmanager.cpp:103

会不会是编译器设置造成的?

I have trouble with using code similar to the following one:

std::map<boost::tuple<int, int, int>, int> m;
boost::tuple<int, int, int> key = boost::make_tuple(1,2,3);
m.find(key);

The compiler does not see any errors. But when I start my program a weird Segmentation fault occurs. So I wanted to find the code line which causes it. GDB then told me:

Program received signal SIGSEGV, Segmentation fault.
0x0809f40a in boost::tuples::detail::lt<boost::tuples::cons<int,
boost::tuples::cons<int, boost::tuples::cons<int, boost::tuples::null_type> > >, 
boost::tuples::cons<int, boost::tuples::cons<int, boost::tuples::cons<int, 
boost::tuples::null_type> > > > (lhs=..., rhs=...)
at /usr/local/lib/boost_1_45_0/boost/tuple/tuple_comparison.hpp:73
73             lt(lhs.get_tail(), rhs.get_tail()));

Unfortunately I could not find any solutions to this kind of problem so far.

Does anybody see what I missed here?

EDIT: So I did some further investigation. The Object which causes the problem is a user-defined one. And actually neither the boost-stuff nor the usage of the map seems to be the reason because the error also occurs with vectors!

class A {
void foo();
private:
    std::vector<int> v;
}
void A::foo() {
    ...
    v = std::vector<int>(); // Here already comes a segfault.
    ...
}

I also tried to reproduce the error in a seperate class. Unfortunately I wasn't able to provoke the error in there.

Now the gdb tells me:

Program received signal SIGSEGV, Segmentation fault.
0x010cae21 in free () from /lib/libc.so.6
(gdb) backtrace 
#0  0x010cae21 in free () from /lib/libc.so.6
#1  0x00fd7441 in operator delete(void*) () from /usr/lib/libstdc++.so.6
#2  0x080668c7 in __gnu_cxx::new_allocator<int>::deallocate (this=0xbfffdb04, 
__p=0x210bf) at /usr/include/c++/4.4/ext/new_allocator.h:95
#3  0x08064b8d in std::_Vector_base<int, std::allocator<int> >::_M_deallocate 
(this=0xbfffdb04, __p=0x210bf, __n=105047440)
at /usr/include/c++/4.4/bits/stl_vector.h:146
#4  0x0806246a in std::_Vector_base<int, std::allocator<int> >::~_Vector_base
(this=0xbfffdb04, __in_chrg=<value optimized out>)
at /usr/include/c++/4.4/bits/stl_vector.h:132
#5  0x080604d4 in std::vector<int, std::allocator<int> >::~vector (this=0xbfffdb04,  
__in_chrg=<value optimized out>)
at /usr/include/c++/4.4/bits/stl_vector.h:313
#6  0x0809e151 in ModelManager::emitSignal (this=0xbffff20f, o=crossroad, r=none, 
restrID=-1, signal=add, id=-5, colStart=-1, colEnd=-1)
at .build_debug/src/model/modelmanager.cpp:103

Could it be caused by the compiler settings?

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

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

发布评论

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

评论(2

○愚か者の日 2024-10-15 21:59:56
#include <map>
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>

int main()
{
    typedef boost::tuple<int, int, int> TTuple3;
    typedef std::map<TTuple3, int> TTupleMap;
    TTupleMap m;
    TTuple3 key1 = boost::make_tuple(1,2,3);
    TTuple3 key2 = boost::make_tuple(1,2,4);
    m[key1] = 1;
    m[key2] = 2;
    TTupleMap::iterator it = m.find(key1);
    if (it == m.end())
        std::cout << "not found" << std::endl;
    else
        std::cout << "found" << std::endl;

    std::cout << m[key1] << std::endl;
    std::cout << m[key2] << std::endl;

    return 0;
}

这对我来说是:

found
1
2

这里没有任何问题。

#include <map>
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>

int main()
{
    typedef boost::tuple<int, int, int> TTuple3;
    typedef std::map<TTuple3, int> TTupleMap;
    TTupleMap m;
    TTuple3 key1 = boost::make_tuple(1,2,3);
    TTuple3 key2 = boost::make_tuple(1,2,4);
    m[key1] = 1;
    m[key2] = 2;
    TTupleMap::iterator it = m.find(key1);
    if (it == m.end())
        std::cout << "not found" << std::endl;
    else
        std::cout << "found" << std::endl;

    std::cout << m[key1] << std::endl;
    std::cout << m[key2] << std::endl;

    return 0;
}

this produce for me:

found
1
2

Nothing wrong here.

转身泪倾城 2024-10-15 21:59:56

您所描述的行为表明您已经损坏了堆或堆栈。我建议使用选项 -Wall -Wextra 重新编译,修复您遇到的任何警告,然后看看是否仍然遇到崩溃。我会密切关注声明为非 void 且不返回值的函数。默认情况下,g++ 不会对此发出警告,并且几乎总是会导致远离错误实际位置的崩溃。 -Wextra 将打开该警告。

The behavior you're describing indicates you've corrupted your heap or stack. I'd suggest recompiling with options -Wall -Wextra, fix any warnings you encounter and then see if you still run into the crash. I'd pay close attention to functions declared non-void that don't return a value. g++ doesn't warn by default for this and will almost always cause a crash away from where the error actually is. -Wextra will turn that warning on.

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