簡體   English   中英

boost :: tuple和std :: map的分段錯誤

[英]Segmentation Faults with boost::tuple and std::map

我在使用類似於以下代碼的代碼時遇到了麻煩:

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-stuff或映射的使用似乎都不是原因,因為矢量也會發生錯誤!

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

可能是由編譯器設置引起的嗎?

#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

沒錯

您描述的行為表明您已損壞堆或堆棧。 我建議使用選項-Wall -Wextra重新編譯,修復遇到的所有警告,然后查看是否仍然崩潰。 我會密切注意聲明為非void的不返回值的函數。 默認情況下,g ++不會對此發出警告,並且幾乎總是會導致崩潰,並遠離錯誤的實際位置。 -Wextra將打開該警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM