【发布时间】:2025-12-20 10:15:11
【问题描述】:
我有以下功能
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 是我自己的命名空间。
【问题讨论】:
-
你能告诉你
node的构造函数吗? -
您显示的代码没有任何问题。您的系统是否涉及动态加载的插件/dylib?如果您的
shared_ptr指向已卸载插件中的破坏代码,您可能会在关闭时遇到此类问题。make_node_ptr是否也以捕获文件范围静态的方式使用?您必须小心shared_ptr延长对象生命周期的方式,可能会导致它们仅在main之外被销毁(可能为时已晚,无法正确销毁对象)。 -
你的函数也可以是
return std::make_shared<node>(); -
@satanik 问题不在于共享指针,而在于 ctor 内部或更深的地方。基本上是在导致循环的最后两行中,查看
insert和insert_if_leq。或者向我们展示这些功能。 :) -
@satanik 更好的是:将您的代码 reduce 简化为仍能显示您的问题的最小示例。大多数情况下,您会发现自己在过程中发现了问题,否则您创建了一小段代码,其他人(很可能)乐于查看并提供帮助。这是一项非常有价值的技能!
标签: c++ segmentation-fault gdb valgrind