从此崩溃启用共享

enable shared from this crash

我正在尝试创建一个网格,每个点都指向网格中的另一个点(成员变量 drain_),可能是它本身。我想为此使用 shared_ptr,因此我需要对 drain 使用 weak_ptr,因为它可以指向自身。

问题是,此代码在 shared_ptr_base.h 文件中崩溃:

// Now that __weak_count is defined we can define this constructor:
  template<_Lock_policy _Lp>
    inline
    __shared_count<_Lp>::__shared_count(const __weak_count<_Lp>& __r)
    : _M_pi(__r._M_pi) <------ HERE <-------
    {
      if (_M_pi != nullptr)
    _M_pi->_M_add_ref_lock();
      else
    __throw_bad_weak_ptr();
    }

有了堆栈:

std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count at shared_ptr_base.h:824 0x418898
std::__shared_ptr::__shared_ptr at shared_ptr_base.h:949 0x418873
std::shared_ptr::shared_ptr() at shared_ptr.h:251 0x418845
std::enable_shared_from_this::shared_from_this() at shared_ptr.h:573 0x41881b
Node::drainInitialization() at point.cpp:43 0x417f3e
Grid::initialize() at openlem.cpp:799 0x407677
Grid::Grid() at openlem.hpp:152 0x4175dd
main() at orogen.cpp:24 0x417361

你知道我做错了什么吗?这是我的代码:

int main(){
    int  m = 12, n = 13;
    Grid grid = Grid ( m, n );
}

grid.hpp:

class Grid
{
private:
    std::vector<std::vector<NodePtr> > lattice_;    // lattice of nodes
}

grid.cpp:

void Grid::initialize ( int m, int n ){
    this->m_ = m;
    this->n = n;
    lattice_.reserve( m );
    for ( int i = 0; i < m; ++i ){
        lattice_.push_back( std::vector<NodePtr>() );
        lattice_[i].reserve( n );
        for ( int j = 0; j < n; ++j ){
//          NodePtr test = std::make_shared<Node>( );
            NodePtr test = std::shared_ptr<Node>( new Node );
            lattice_[i].push_back( std::move( test ) );
            test->drainInitialization();

            printf( "%i %i \n", lattice_.size(), lattice_[0].size() );
            printf( "%i %i \n", i, j );
        }
    }
}

node.hpp:

class Node : public std::enable_shared_from_this<Node>
{
    typedef std::shared_ptr<Node> NodePtr;
public:
    Node (); // Constructor
    void drainInitialization();
    ~Node (); // Destructor

    int          i; // Coordinate
    int          j; // Coordinate
    std::weak_ptr<Node> drain_;   // flow target
}

node.cpp:

Node::Node(): i(init_int), j(init_int), elevation_(init_double),
chann_el_slope_(init_double), chann_wa_slope_(init_double),
water_level_(init_double), uplift_(init_float), discharge_(init_uint),
border_(0), marker_(0){}

void Node::drainInitialization(){
    drain_ = shared_from_this();
}

lattice_[i].push_back( std::move( test ) )之后,test不再有效,是一个空指针。不用移动shared_ptr,直接复制到vector里面就可以了

或者您仍然可以移动到向量中,但随后使用 lattice_[i].back()->drainInitialization() 而不是 test->drainInitialization()