[英]std::bad_function_call of self-calling recursive lambda but no hints
I'm using lldb and llvm (clang 6.0) on OSX. 我在OSX上使用lldb和llvm(clang 6.0)。 The following code throws an std::bad_function_call at line 30:
以下代码在第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
}
}
};
This is the caller, right below the definition of the lambda: 这是调用者,正好在lambda的定义之下:
for ( const auto lhs : _edges )
{
unsigned int len = 0;
const auto from = lhs.to;
find_next ( from, len );
}
And this is the lldb output: 这是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
Running a backtrace: 运行回溯:
(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
I capture everything by value, as the iterator seems to invalidate the pointer if I capture by reference. 我按值捕获所有内容,因为如果我通过引用捕获迭代器似乎使指针无效。
What I do not understand, is that the recursive call at line 30, I get the hint from lldb ( frame #9: 0x000000010009edfd ) that node_to is not available. 我不明白的是,第30行的递归调用,我从lldb( 帧#9:0x000000010009edfd )得到提示,即node_to不可用。 However the assertion doesn't catch this, but only instead an std::bad_function_call is thrown, which if I understand correctly, means that the function I am calling is invalid?
但是断言并没有捕获到这一点,而只是抛出std :: bad_function_call,如果我理解正确的话,意味着我调用的函数是无效的?
EDIT: I made a minified example: on ideone.com 编辑:我做了一个缩小的例子: 在ideone.com上
You're capturing find_next
by value, before assigning the lambda to it, while it's still empty. 在将lambda分配给它之前,你正在按值捕获
find_next
,而它仍然是空的。
Capturing by reference should work, as long as you don't need to copy the function and use it after destroying the original. 只要您不需要复制功能并在销毁原件后使用它,通过引用捕获应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.