我在OSX上使用lldb和llvm(clang 6.0)。 以下代码在第30行抛出std :: bad_function_call:
std::function<void ( const std::shared_ptr<Node>, unsigned int )> find_next;
find_next = [=]( const std::shared_ptr<Node> node_to, unsigned int len )
{
for ( const auto rhs : _edges )
{
assert(node_to);
assert(rhs.from);
if ( (*node_to) == (*rhs.from) )
{
len++;
find_next ( rhs.from, len ); // line 30
}
}
};
这是调用者,就在lambda的定义下面:
for ( const auto lhs : _edges )
{
unsigned int len = 0;
const auto from = lhs.to;
find_next ( from, len );
}
这是lldb输出:
libc++abi.dylib: terminating with uncaught exception of type std::__1::bad_function_call: std::exception
Avg Path Length: Process 4369 stopped
* thread #1: tid = 0x17220, 0x00007fff8c2bd282 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00007fff8c2bd282 libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill + 10:
-> 0x7fff8c2bd282: jae 0x7fff8c2bd28c ; __pthread_kill + 20
0x7fff8c2bd284: movq %rax, %rdi
0x7fff8c2bd287: jmp 0x7fff8c2b8ca3 ; cerror_nocancel
0x7fff8c2bd28c: retq
运行回溯:
(lldb) bt
* thread #1: tid = 0x17220, 0x00007fff8c2bd282 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
* frame #0: 0x00007fff8c2bd282 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x00007fff8bca24c3 libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x00007fff918ebb73 libsystem_c.dylib`abort + 129
frame #3: 0x00007fff8ba8ea21 libc++abi.dylib`abort_message + 257
frame #4: 0x00007fff8bab69b9 libc++abi.dylib`default_terminate_handler() + 243
frame #5: 0x00007fff8c3f96db libobjc.A.dylib`_objc_terminate() + 124
frame #6: 0x00007fff8bab40a1 libc++abi.dylib`std::__terminate(void (*)()) + 8
frame #7: 0x00007fff8bab3b30 libc++abi.dylib`__cxa_throw + 121
frame #8: 0x000000010009f453 extras`std::__1::function<void (this=0x0000000100400370, __arg=shared_ptr<cgpp::Node> at 0x00007fff5fbfec28, __arg=1)>::operator()(std::__1::shared_ptr<cgpp::Node>, unsigned int) const + 131 at functional:1753
frame #9: 0x000000010009edfd extras`cgpp::ConceptualGraph::avgPathLength(this=0x0000000100400360, node_to=<unavailable>, len=1) const::$_0::operator()(std::__1::shared_ptr<cgpp::Node>, unsigned int) const + 925 at ConceptualGraphEXTRAS.cpp:30
frame #10: 0x000000010009e992 extras`std::__1::__function::__func<cgpp::ConceptualGraph::avgPathLength() const::$_0, std::__1::allocator<cgpp::ConceptualGraph::avgPathLength() const::$_0>, void (std::__1::shared_ptr<cgpp::Node>, unsigned int)>::operator()(std::__1::shared_ptr<cgpp::Node>&&, unsigned int&&) [inlined] decltype(this=0x0000000100400360, __f=0x0000000100400360, __args=0x00007fff5fbfef50, __args=0x00007fff5fbfeeb4) const::$_0&>(fp)(std::__1::forward<std::__1::shared_ptr<cgpp::Node>, unsigned int>(fp0))) std::__1::__invoke<cgpp::ConceptualGraph::avgPathLength() const::$_0&, std::__1::shared_ptr<cgpp::Node>, unsigned int>(cgpp::ConceptualGraph::avgPathLength() const::$_0&&&, std::__1::shared_ptr<cgpp::Node>&&, unsigned int&&) + 119 at __functional_base:413
frame #11: 0x000000010009e91b extras`std::__1::__function::__func<cgpp::ConceptualGraph::avgPathLength(this=0x0000000100400350, __arg=0x00007fff5fbfef50, __arg=0x00007fff5fbfeeb4) const::$_0, std::__1::allocator<cgpp::ConceptualGraph::avgPathLength() const::$_0>, void (std::__1::shared_ptr<cgpp::Node>, unsigned int)>::operator()(std::__1::shared_ptr<cgpp::Node>&&, unsigned int&&) + 91 at functional:1370
frame #12: 0x000000010009f48d extras`std::__1::function<void (this=0x00007fff5fbff340, __arg=shared_ptr<cgpp::Node> at 0x00007fff5fbfef50, __arg=0)>::operator()(std::__1::shared_ptr<cgpp::Node>, unsigned int) const + 189 at functional:1755
frame #13: 0x000000010009cc89 extras`cgpp::ConceptualGraph::avgPathLength(this=0x0000000100304e38) const + 1065 at ConceptualGraphEXTRAS.cpp:40
frame #14: 0x00000001000029e6 extras`main + 3334 at extras.cpp:37
frame #15: 0x00007fff869b15c9 libdyld.dylib`start + 1
frame #16: 0x00007fff869b15c9 libdyld.dylib`start + 1
我按值捕获所有内容,因为如果我通过引用捕获迭代器似乎使指针无效。
我不明白,是第30行的递归调用,我从lldb(帧#9:0x000000010009edfd )得到 node_to 不可用的提示。然而,断言没有捕获到这一点,但只是抛出了std :: bad_function_call,如果我理解正确,则意味着我调用的函数无效?
编辑:我做了一个缩小的例子:on ideone.com
答案 0 :(得分:9)
在将lambda分配给它之前,你按值捕获find_next
,而它仍然是空的。
通过引用捕获应该有效,只要您不需要复制该函数并在销毁原始文件后使用它。