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-14103] Memory leak when pattern matching some tuples to Optional<Any>.none in switch #56489

Closed
JosephDuffy opened this issue Jan 25, 2021 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@JosephDuffy
Copy link

Previous ID SR-14103
Radar rdar://problem/73573344
Original Reporter @JosephDuffy
Type Bug
Status Resolved
Resolution Duplicate
Environment

Tested with Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28) using Xcode 12.3 and Xcode 12.4.

iOS 13.0 simulator, iOS 14.4 simulator, iOS 12.4 real device

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 08185f64820c0384e60d3647f7180e11

Issue Description:

We've been running in to very high memory usage and I think I've tracked it down. The following code will cause memory usage to continuously grow:

func checkIfNil(_ value: Any) -> Bool {
    switch value {
    case Optional<Any>.none: return true
    default: return false
    }
}

let wrapped = ("bar", Int?.some(2))
while true {
    checkIfNil(wrapped)
}

There are a few different type of tuples that trigger this:

  • (String, Int?)

  • (String?, Int?)

  • (String?, Int?, Int)

  • (String, Int, Int?)

  • (String, Int?, Int?)

  • (String, Int?, Int)

  • (String?, Int, Int)

  • (String, Int, Int)

The types within the tuple don't seem to matter, only the optionality seems to matter. The issue also occurs whether the tuple itself is optional or not.

There are also a few types that don't trigger this:

  • (String?, Int)

  • (String, Int)

Xcode's memory graph debugger shows the issue as coming from:

case Optional<Any>.none: return true

When the `value` is `nil` (e.g. `Wrapper<(String, Int?)>(value: nil)`) it does not cause a memory leak.

A workaround for this is to use `if case`:

if case Optional<Any>.none = value as Any {
    return true
} else {
    return false
}
@typesanitizer
Copy link

@swift-ci create

@eeckstein
Copy link
Member

This is a duplicate of https://bugs.swift.org/browse/SR-13926, which will be fixed in Swift 5.4

@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
Projects
None yet
Development

No branches or pull requests

3 participants