Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9194] expression evaluation permanently retains objects #4350

Open
kastiglione opened this issue Nov 5, 2018 · 4 comments
Open

[SR-9194] expression evaluation permanently retains objects #4350

kastiglione opened this issue Nov 5, 2018 · 4 comments
Labels
bug Something isn't working LLDB for Swift

Comments

@kastiglione
Copy link

Previous ID SR-9194
Radar rdar://problem/23342985
Original Reporter @kastiglione
Type Bug
Additional Detail from JIRA
Votes 0
Component/s LLDB for Swift
Labels Bug
Assignee None
Priority Medium

md5: eb0ae1889a3f14e971850fa9bde9e1b2

Issue Description:

When evaluating an expression that returns an object, the object will be permanently retained. This is true both using the expression command and using SBFrame.EvaluateExpression(). From the perspective of using po, this was quite the surprise.

My guess is that this is because of persistent variables. I tried to see if this could be worked around using Python, but the following did not work:

opt = lldb.SBExpressionOptions()
opt.SetSuppressPersistentResult(True)
value = frame.EvaluateExpression("self", opt)
print value.path

The output of value.path is $Rn. So it appears this may not actually be suppressing the persistent variable. This may be a separate but related bug.

If this is caused by persistent variables, note that po does not print the name of the persistent variable that was created. Which leads to a question: should po always print the name of the persistent variables created, or should po always disable persistent variables?

@belkadan
Copy link

belkadan commented Nov 6, 2018

jingham@apple.com (JIRA User), you have a dup of this, right? Or is it just a Radar?

@swift-ci
Copy link

swift-ci commented Nov 6, 2018

Comment by Jim Ingham (JIRA)

I just have a Radar, I linked it to this JIRA.

@swift-ci
Copy link

swift-ci commented Nov 6, 2018

Comment by Jim Ingham (JIRA)

This isn't terribly important, but SetSuppressPersistentResult doesn't cause a result for the expression not to be produced, all it means is that the name isn't added to the list of known result names for use in future expressions. We still produce the result, and in the case of "po" we need too since we call description on the result as a second step.

The name of the variable that is produced is the one it would have had, but if you try to use that name in a future expression you will get an error. This is a C example, but Swift is the same:

(lldb) script
>>> options = lldb.SBExpressionOptions()
>>> options.SetSuppressPersistentResult(True)
>>> value = lldb.frame.EvaluateExpression('printf("Hello there")', options)
>>> print value.GetError().Success()
True
>>> print value.GetName()
$0
>>> print value.GetValueAsUnsigned()
11
>>> quit
(lldb) expr $0 + 11
error: use of undeclared identifier '$0'

The "result variables" in lldb are constant objects that we store on the lldb side, and re-inject as values if they are needed in subsequent expressions. So suppressing them is just about removing the accounting on lldb's side.

Anyway, the code that is retaining the result object is actually part of the expression wrapper that we compile and run to implement calling your expression. We capture the result of the expression by assigning it to a known variable which we read once the expression is over. When we do that swift inserts a retain.

What we should really do is to figure out how to capture the result w/o retaining it. That's probably not too hard if the result is a reference to an object. But would be tricky, for instance if the expression was a function call that returned an unretained object, since that might go away at the end of the expression evaluation before we could get our hands on it. I'm actually not sure of an easy way to fix this.

@kastiglione
Copy link
Author

Thanks for the helpful clarifications!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working LLDB for Swift
Projects
None yet
Development

No branches or pull requests

3 participants