C++ 创建共享_ptr似乎会导致SEGFULT
我有以下功能C++ 创建共享_ptr似乎会导致SEGFULT,c++,segmentation-fault,gdb,valgrind,C++,Segmentation Fault,Gdb,Valgrind,我有以下功能 static node_ptr make_node_ptr() { return node_ptr(new node()); } 其中使用node_ptr=std::shared_ptr 我试图找出valgrind和gdb的细分错误。在这两种情况下,我得到的堆栈跟踪大致相同 Program received signal SIGSEGV, Segmentation fault. 0x00007fff8f5d7e82 in szone_malloc_should_clear (
static node_ptr make_node_ptr()
{
return node_ptr(new node());
}
其中使用node_ptr=std::shared_ptr代码>
我试图找出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
程序接收信号SIGSEGV,分段故障。
szone_malloc_中的0x00007fff8f5d7e82应该从/usr/lib/system/libsystem_malloc.dylib中清除()
(gdb)英国电信
#szone_malloc_中的0 0x00007fff8f5d7e82应该从/usr/lib/system/libsystem_malloc.dylib中清除()
#1 0x00007fff8f5d7877位于/usr/lib/system/libsystem\u malloc.dylib中的malloc_zone_malloc()中
#来自/usr/lib/system/libsystem\u malloc.dylib的malloc()中的2 0x00007fff8f5d6395
#来自/usr/local/lib/gcc/4.9/libstdc++.6.dylib的运算符new(无符号长)()中的3 0x00000001000f17d8
#标准中的4 0x0000000100009c04:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
at/usr/local/ceral/gcc49/4.9.2_1/include/c++/4.9.2/bits/shared_ptr_base.h:569
#标准中的5 0x0000000100008c78:\uuuuuuu共享\uu ptr::\uuuuuu共享\uuptr(此=0x7FFF5002E0,\uuuuuu p=0x10059ffc0)
at/usr/local/ceral/gcc49/4.9.2_1/include/c++/4.9.2/bits/shared_ptr_base.h:871
#std::shared_ptr::shared_ptr中的6 0x00000001000079e5(此=0x7FFF5002E0,_p=0x10059ffc0)
at/usr/local/ceral/gcc49/4.9.2_1/include/c++/4.9.2/bits/shared_ptr.h:113
#7 0x0000000100005bdc位于.././common/msc/scene.d/scene_manager_def.h:98的msc::scene_manager::make_node_ptr()中
#在.././common/msc/scene.d/node_impl.h:73处的msc::scene_manager::node::generate_wrappee_node(wrappee=…)中的8 0x00000001000037dd
#9 0x0000000100003d53位于.././common/msc/scene.d/node_impl.h:112处的msc::scene_manager::node::generate_wrapping_node(nodes=…)中
#10 0x0000000100004011位于.././common/msc/scene.d/scene\u manager\u impl.h:97处的msc::scene\u manager::insert(this=0x7fff5f82ee90,root=…,other=…)中
#11 0x0000000100006071在msc::scene_manager::insert_if_leq(this=0x7fff5f82ee90,root=…,other=…)中位于.././common/msc/scene.d/scene_manager_impl.h:127
我想,至少在我试图继续到6000帧或smth时,最后两行会不断重复
我是否遗漏了什么,或者不允许创建此共享\u 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;
}
node_ptr scene_manager::insert(node_ptr&root,node_ptr other)
{
布尔交换=假;
if(root==nullptr)
根=其他;
其他的
{
自动内部=msc::内部::测试(*根,*其他);
如果(内部==msc::内部::无)
{
自动oldRoot=root;
根=节点::生成\u包装\u节点(
std::向量{oldRoot,other});
插入_if_leq(根,旧根);
}
如果((交换=(内部==msc::内部::第一次)),则为else
{
标准::交换(根,其他);
}
其他=插入(根,其他);
}
返回!交换?其他:根目录;
}
节点\u ptr场景\u管理器::插入\u if\u leq(节点\u ptr根,节点\u ptr其他)
{
node_ptr res=根;
如果(类似(*根,*其他))
msc::move_append(根->子对象,其他->子对象);
其他的
{
自动idx=根->获取象限\U idx(其他->中心);
res=插入(根->象限[idx],其他);
}
返回res;
}
这些是重复的功能msc
是我自己的名称空间。由于函数是递归的insert
调用insert\u if\u leq
,反之亦然,我必须将insert\u if\u leq
中的node\u ptr root
作为引用,因为insert
中的赋值依赖于它。新的函数定义如下:
node_ptr scene_manager::insert(node_ptr&root,node_ptr other)
node\u ptr scene\u manager::insert\u if\u leq(node\u ptr&root,node\u ptr other)
能否显示节点的构造函数?显示的代码没有问题。您的系统是否包含动态加载的插件/动态库?如果您的shared_ptr
指向已卸载插件中的销毁代码,则在关机时可能会遇到此类问题。另外,make\u node\u ptr
是否以捕获文件范围静态的方式使用?您必须小心shared_ptr
延长对象生命周期的方式,可能会导致它们只在main
之外被销毁(可能是一个太晚而无法正确销毁对象的点)。此外,您的函数可能只是返回std::make_shared()
@satanik问题不在于共享指针,而在于ctor内部或更深层的某个地方。基本上是在最后两行引起循环,看看insert
和insert\u if\u leq
。或者向我们展示这些功能。:)@satanik更好:把你的代码减少到最小的例子来说明你的问题。大多数情况下,您会发现自己在这个过程中发现了问题,否则您已经创建了一小段代码,其他人(很可能)很乐意看到并提供帮助。这是一项非常宝贵的技能!