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-972] Memory leak when using try? #43584

Closed
keith opened this issue Mar 17, 2016 · 6 comments
Closed

[SR-972] Memory leak when using try? #43584

keith opened this issue Mar 17, 2016 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself SILGen Area → compiler: The SIL generation stage

Comments

@keith
Copy link
Collaborator

keith commented Mar 17, 2016

Previous ID SR-972
Radar None
Original Reporter @keith
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, SILGen
Assignee None
Priority Medium

md5: 569d1a0a8ce41b1db20ee5b587fead55

duplicates:

  • SR-919 Memory Leak using "try?"

Issue Description:

We've discovered an interesting memory leak when using `try?`. Here's an example to reproduce it:

struct Error: ErrorType {}

func something() throws {
    throw Error()
}

for _ in 0..<10000000 {
    // Leaks if you use `try?`
    _ = try? something()

    // Doesn't leak if you handle the error
    // do {
    //     try something()
    // } catch {}
}

To see this in action you can compile this with `swiftc` and then execute it through instruments using the leaks tool. We're seeing this in both Swift 2.1 and the most recent 2.2 beta bundled with the Xcode 7.3 beta.

@belkadan
Copy link
Contributor

@slavapestov, did you fix something here recently?

@dduan
Copy link
Collaborator

dduan commented Mar 18, 2016

Master still has this BTW.

@swift-ci
Copy link
Collaborator

Comment by Martin Conte Mac Donell (JIRA)

The problem seems to be that the catch block is being emitted with an error argument that is not being released (here: https://github.com/apple/swift/blob/master/lib/SILGen/SILGenExpr.cpp#L1000)

@belkadan Is it safe to emit a strong release right after the createBBArg?. That emits the right SIL but I'm not sure that's the right way of fixing this. Maybe you can shed some light and I can submit a PR

@slavapestov
Copy link
Member

Martin, that sounds about right. Ideally we would try to use the ManagedValue abstraction instead of emitting a release directly. I'll take a look right now.

@keith
Copy link
Collaborator Author

keith commented Mar 21, 2016

For future readers: #1740

@belkadan
Copy link
Contributor

Consolidating.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself SILGen Area → compiler: The SIL generation stage
Projects
None yet
Development

No branches or pull requests

5 participants