std :: bad_function_call自调用递归lambda但没有提示

Æle*_*lex 2 c++ recursion lambda c++11

我在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
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

这是调用者,正好在lambda的定义之下:

for ( const auto lhs : _edges )
{
    unsigned int len = 0;
    const auto from = lhs.to;
    find_next ( from, len );
}
Run Code Online (Sandbox Code Playgroud)

这是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  
Run Code Online (Sandbox Code Playgroud)

运行回溯:

(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
Run Code Online (Sandbox Code Playgroud)

我按值捕获所有内容,因为如果我通过引用捕获迭代器似乎使指针无效.

我不明白的是,第30行的递归调用,我从lldb(第9帧:0x000000010009edfd)获得了node_to不可用的提示.但是断言并没有捕获到这一点,而只是抛出std :: bad_function_call,如果我理解正确的话,意味着我调用的函数是无效的?

编辑:我做了一个缩小的例子:在ideone.com上

Mik*_*our 9

find_next在将lambda分配给它之前,你是按值捕获的,而它仍然是空的.

只要您不需要复制功能并在销毁原件后使用它,通过引用捕获应该可以工作.

  • @Alex不,你有无限的递归.不是无限循环.无限递归会破坏您的堆栈,并在发生这种情况时导致未定义的行为:无限循环会使您的代码通常挂起.在编写递归代码时,请确保您的调用终止,并确保它不会太深(堆栈通常只有几MB). (3认同)