创建共享_ptr似乎会导致segfault



i具有以下功能

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作为参考,因为insert中的分配取决于它。新功能定义如下:

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

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

相关内容

  • 没有找到相关文章

最新更新