std :: list :: clear导致segfault(内存地址无效)

时间:2013-10-22 21:47:48

标签: c++ stl

我有ObjRecRANSAC类,它有以下std :: list成员:

    std::list<OrientedPointPair> sampled_oriented_point_pairs_;

OrientedPointPair是ObjRecRANSAC中定义的类,如下所示:

    class OrientedPointPair
    {
        public:
          OrientedPointPair (const float *p1, const float *n1, const float *p2, const float *n2)
          : p1_ (p1), n1_ (n1), p2_ (p2), n2_ (n2)
          {
          }

          virtual ~OrientedPointPair (){}

        public:
          const float *p1_, *n1_, *p2_, *n2_;
    };

构建ObjRecRANSAC类之后。调用sample_oriented_point_pairs_.clear()会导致seg错误。

我尝试使用gdb来调试错误,这就是我得到的:

print sampled_oriented_point_pairs_
$4 = {<std::_List_base<pcl::recognition::ObjRecRANSAC::OrientedPointPair, std::allocator<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >> = {
    _M_impl = {<std::allocator<std::_List_node<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >> = {<__gnu_cxx::new_allocator<std::_List_node<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >> = {<No data fields>}, <No data fields>}, _M_node = {
        _M_next = 0x3f7d70a4, _M_prev = 0x0}}}, <No data fields>}
(gdb) s
std::list<pcl::recognition::ObjRecRANSAC::OrientedPointPair, std::allocator<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >::clear (this=0x7fffffffdb38) at /usr/include/c++/4.6/bits/stl_list.h:1197
1197            _Base::_M_clear();
(gdb) s
std::_List_base<pcl::recognition::ObjRecRANSAC::OrientedPointPair, std::allocator<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >::_M_clear (this=0x7fffffffdb38) at /usr/include/c++/4.6/bits/list.tcc:70
70        _Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);
(gdb) print __cur
$5 = (
    std::_List_base<pcl::recognition::ObjRecRANSAC::OrientedPointPair, std::allocator<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >::_Node *) 0x7a6c60
(gdb) print *__cur
$6 = {<std::__detail::_List_node_base> = {_M_next = 0x50a330, _M_prev = 0x7fff00000000}, _M_data = {_vptr.OrientedPointPair = 0x0, 
    p1_ = 0x7fffef9364d8, n1_ = 0x1b280e0, p2_ = 0x1ed2f00, n2_ = 0x1ed2f00}}
(gdb) s
71        while (__cur != &this->_M_impl._M_node)
(gdb) s
73        _Node* __tmp = __cur;
(gdb) s
74        __cur = static_cast<_Node*>(__cur->_M_next);
(gdb) print __cur
$7 = (
    std::_List_base<pcl::recognition::ObjRecRANSAC::OrientedPointPair, std::allocator<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >::_Node *) 0x3f7d70a4
(gdb) print *__cur
** Cannot access memory at address 0x3f7d70a4 **
(gdb) print &this->_M_impl._M_node
$8 = (std::__detail::_List_node_base *) 0x7fffffffdb38
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0x00000000004c1608 in std::_List_base<pcl::recognition::ObjRecRANSAC::OrientedPointPair, std::allocator<pcl::recognition::ObjRecRANSAC::OrientedPointPair> >::_M_clear (this=0x7fffffffdb38) at /usr/include/c++/4.6/bits/list.tcc:74
74        __cur = static_cast<_Node*>(__cur->_M_next);

显然这是问题:无法访问地址0x3f7d70a4的内存

我还不明白为什么__cur的地址在执行第70行之前最初设置为0x7a6c60,此时甚至没有声明当前。在执行第70行之前打印*__cur似乎也返回一个值,这也是一个惊喜。

samples_oriented_point_pair_只是在类中声明,并且未在构造函数中初始化。这可能是问题吗?但是,这段代码以前用过吗?

0 个答案:

没有答案