创建 shared_ptr 似乎会导致段错误

Creating shared_ptr seems to cause segfault

我有以下功能

static node_ptr make_node_ptr()
{
  return node_ptr(new node());
}

其中 using node_ptr = std::shared_ptr<node>;

我试图用 valgrind 和 gdb 找出我的分段错误。在两者中,我或多或少得到了相同的堆栈跟踪。

Program received signal SIGSEGV, Segmentation fault.
0x00007fff8f5d7e82 in szone_malloc_should_clear () from /usr/lib/system/libsystem_malloc.dylib
(gdb) bt
#0  0x00007fff8f5d7e82 in szone_malloc_should_clear () from /usr/lib/system/libsystem_malloc.dylib
#1  0x00007fff8f5d7877 in malloc_zone_malloc () from /usr/lib/system/libsystem_malloc.dylib
#2  0x00007fff8f5d6395 in malloc () from /usr/lib/system/libsystem_malloc.dylib
#3  0x00000001000f17d8 in operator new(unsigned long) () from /usr/local/lib/gcc/4.9/libstdc++.6.dylib
#4  0x0000000100009c04 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<msc::scene_manager<float, int, 3ul>::node*> (this=0x7fff5f4002e8, __p=0x10059ffc0)
    at /usr/local/Cellar/gcc49/4.9.2_1/include/c++/4.9.2/bits/shared_ptr_base.h:569
#5  0x0000000100008c78 in std::__shared_ptr<msc::scene_manager<float, int, 3ul>::node, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<msc::scene_manager<float, int, 3ul>::node> (this=0x7fff5f4002e0, __p=0x10059ffc0)
    at /usr/local/Cellar/gcc49/4.9.2_1/include/c++/4.9.2/bits/shared_ptr_base.h:871
#6  0x00000001000079e5 in std::shared_ptr<msc::scene_manager<float, int, 3ul>::node>::shared_ptr<msc::scene_manager<float, int, 3ul>::node> (this=0x7fff5f4002e0, __p=0x10059ffc0)
    at /usr/local/Cellar/gcc49/4.9.2_1/include/c++/4.9.2/bits/shared_ptr.h:113
#7  0x0000000100005bdc in msc::scene_manager<float, int, 3ul>::make_node_ptr () at ../../common/msc/scene.d/scene_manager_def.h:98
#8  0x00000001000037dd in msc::scene_manager<float, int, 3ul>::node::generate_wrapping_node (wrappee=...) at ../../common/msc/scene.d/node_impl.h:73
#9  0x0000000100003d53 in msc::scene_manager<float, int, 3ul>::node::generate_wrapping_node (nodes=...) at ../../common/msc/scene.d/node_impl.h:112
#10 0x0000000100004011 in msc::scene_manager<float, int, 3ul>::insert (this=0x7fff5f82ee90, root=..., other=...) at ../../common/msc/scene.d/scene_manager_impl.h:97
#11 0x0000000100006071 in msc::scene_manager<float, int, 3ul>::insert_if_leq (this=0x7fff5f82ee90, root=..., other=...) at ../../common/msc/scene.d/scene_manager_impl.h:127

最后两行无休止地重复,我想至少,因为我试图继续直到帧 #6000 或 smth。

我是不是遗漏了什么或者不允许创建这个 shared_ptr?

编辑

node_ptr scene_manager::insert(node_ptr & root, node_ptr other)
{
  bool swapped = false;

  if (root == nullptr)
    root = other;
  else
  {
    auto inside = msc::inside::test(*root, *other);
    if (inside == msc::inside::NONE)
    {
      auto oldRoot = root;
      root = node::generate_wrapping_node(
        std::vector<node_c_ptr>{ oldRoot, other });
      insert_if_leq(root, oldRoot);
    }
    else if ((swapped = (inside == msc::inside::FIRST)))
    {
      std::swap(root, other);
    }
    other = insert_if_leq(root, other);
  }

  return !swapped ? other : root;
}

node_ptr scene_manager::insert_if_leq(node_ptr root, node_ptr other)
{
  node_ptr res = root;

  if (are_similar(*root, *other))
    msc::move_append(root->children, other->children);
  else
  {
    auto idx = root->get_quadrant_idx(other->center);
    res = insert(root->quadrants[idx], other);
  }

  return res;
}

这些是重复的函数。 msc 是我自己的命名空间。

由于函数是递归的 insert 调用 insert_if_leq 反之亦然,我不得不在 insert_if_leq 中创建 node_ptr root 一个引用,因为 [=10] 中的赋值=] 取决于它。新函数定义如下:

node_ptr scene_manager::insert(node_ptr & root, node_ptr other)

node_ptr scene_manager::insert_if_leq(node_ptr & root, node_ptr other)