21

I stuck with an interesting behavior of Xcode LLDB debug console. When I use weak self + guard self statement for preventing memory leaks then I run into strange behavior while debugging my code when trying to print closure parameter (like the response in the example) or anything class/struct property from closure. It is an example of closure and line with print statement has a breakpoint on which I am trying to print response parameter from Xcode console.

Alamofire.request(url).responseJSON { [weak self] response in
    guard let self = self else { return }
    print("This line has a breakpoint and I am trying to print response from debug console")
}

I try to use such commands:

po response
p response
print response
e response
expression response

And always I get an error like this:

    (lldb) po response
error: warning: <EXPR>:12:9: warning: initialization of variable '$__lldb_error_result' was never used; consider replacing with assignment to '_' or removing it
    var $__lldb_error_result = __lldb_tmp_error
    ~~~~^~~~~~~~~~~~~~~~~~~~
    _

error: <EXPR>:18:5: error: value of type 'APIManager' has no member '$__lldb_wrapped_expr_25'
    $__lldb_injected_self.$__lldb_wrapped_expr_25(     
    ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~

Did you stuck with that problem too? Why I get that error and what are possible workarounds except removing weak self + guard self statement or inserting print(response) into the closure code?

Temporary solution:

Don't use self shadowing, instead use variable name something like this _self then you don't get lldb errors like above:

Alamofire.request(url).responseJSON { [weak self] response in
    guard let _self = self else { return }
    // your code here ...
}

Or use v LLDB command instead of po. v command has a bit different output formatting, but it should show you needed info for most cases.

Community
  • 1
  • 1
m3rk
  • 653
  • 6
  • 12
  • 2
    Hi! take a look at this bug report - https://bugs.swift.org/browse/SR-6156 - there is an alternative to visualize your variables with python+lldb – andreskwan Mar 05 '18 at 04:29
  • Does this answer your question? [Xcode lldb error: can't print out Swift variable - get "$\_\_lldb\_injected\_self.$\_\_lldb\_wrapped\_expr\_x" instead](https://stackoverflow.com/questions/48245427/xcode-lldb-error-cant-print-out-swift-variable-get-lldb-injected-self) – Kamil.S Mar 06 '20 at 19:57

1 Answers1

1

Though you've likely long forgotten about this question, my first guess is that it's a compiler optimization, because you aren't using the response parameter anywhere in the closure.

@matt has a bit more about this in the comments.

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • 2
    No that’s not right any more. Even in a debug build, an unused value is not in scope in LLDB in an asynchronous closure. – matt Jun 24 '20 at 02:20
  • Thank for catching me on that @matt. I wasn't paying close attn, and also _did not know that_. I rolled it back to the original. Regardless, I'm _definitely not right_, but that's outside SO scope. ;) – Clay Bridges Jun 24 '20 at 02:25