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-6536] Creating a string description of a Swift.Error with an associated optional Swift.Error leaks a _SwiftNativeNSError (80 bytes) #49086

Closed
swift-ci opened this issue Dec 5, 2017 · 9 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 5, 2017

Previous ID SR-6536
Radar rdar://problem/35856344
Original Reporter manuelburghard (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee manuelburghard (JIRA)
Priority Medium

md5: 516259d527c380fec26379fe9db0bea3

Issue Description:

Executing the following code will always leak an instance of _SwiftNativeNSError (80 bytes).

public enum A: Error {
    case a(Error?)
}

public enum B: Error {
    case b
}

_ = String(describing: A.a(B.b))

Steps to reproduce:

  1. Execute the code above.

  2. Use the Memory Graph Debugger to create a snapshot

  3. Check the runtime issues tab in Xcode.

Verified in Xcode 9.1 and Xcode 9.2

@jckarter
Copy link
Member

jckarter commented Dec 5, 2017

@swift-ci create

@jckarter
Copy link
Member

jckarter commented Dec 5, 2017

It looks like `A.a` has to have an `Optional` payload to trigger the leak. Making `B` CustomDebugStringConvertible seems to work around the leak.

@jckarter
Copy link
Member

jckarter commented Dec 5, 2017

It also looks like `B` has to specifically be an enum, as opposed to a struct. Slightly reduced repro case:

import Foundation

public enum B: Error { case b }

autoreleasepool {
  print(B.b as Error?)
}

@jckarter
Copy link
Member

jckarter commented Dec 5, 2017

Leak appears to be independent of the `Error` protocol as well. This also reproduces the leak for me:

import Foundation

protocol P {}

public enum B: P { case b }

autoreleasepool {
  print(B.b as P?)
}

@jckarter
Copy link
Member

jckarter commented Dec 5, 2017

Leak seems to be caused somewhere during mirror traversal. This also leaks, using `dump` instead of `print`:

import Foundation

protocol P {}

public enum B: P {
  case b
}

autoreleasepool {
  dump(B.b as P)
}

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 7, 2017

Comment by James Froggatt (JIRA)

I'm getting something like this bug as well, without Optional:

public enum ANTLRException: Error {
    case parseCancellation(e: RecognitionException)
    case recognition(e: RecognitionException)
}

Here, `RecognitionException` doesn't conform to `Error`. Attempting to catch the `ANTLRException` error (from another module) gives a `_SwiftNativeNSError`, and attempts to cast to `ANTLRException` fail. Attempts to print or get the localizedDescription also result in a crash.

@jckarter
Copy link
Member

SAJim (JIRA User) Can you file a new bug for that? Looking at the presence of _SwiftNativeNSError itself is probably misleading, since that is the representation for all values of Error type in Swift. It should be an implementation detail.

@swift-ci
Copy link
Collaborator Author

Comment by James Froggatt (JIRA)

Thanks Joe, it did turn out to be a completely different problem. I'll file a report when I have a moment.

@jckarter
Copy link
Member

jckarter commented Jan 8, 2018

@slavapestov fixed a runtime leak in #13792 that's probably responsible for this.

@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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants