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-14326] Local variables has been released before the end of scope #4218

Open
swift-ci opened this issue Mar 11, 2021 · 0 comments
Open

[SR-14326] Local variables has been released before the end of scope #4218

swift-ci opened this issue Mar 11, 2021 · 0 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-14326
Radar None
Original Reporter Ray999 (JIRA User)
Type Bug
Environment

macOS 10.15.7 (19H2)
Xcode 12.4 (12D4e)
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin19.6.0

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

md5: 35d5828be2f7cc590e207d5b1f16599c

Issue Description:

There is my code.

enum AnimalType {
    case dog
    case cat
}

protocol Animal: AnyObject {
    var description: String { get }
}

class Dog: Animal {
    var description: String { return "Dog" }
}

class Cat: Animal {
    var description: String { return "Cat" }
}

class Foo {
    weak var animal: Animal?
}

func f1(animalType: AnimalType) {
    let animal: Animal
    switch animalType {
    case .dog:
        animal = Dog()
    case .cat:
        animal = Cat()
    }
    
    let foo = Foo()
    foo.animal = animal
  
    if let a = foo.animal {
        print(a.description) // Debug
    } else {
        print("animal has been released") // Release
    }
}

f1(animalType: .dog)

This code can work in DEBUG mode, but not in RELEASE mode.

I add `init` and `deinit` for those class, and find local variable animal has been released right after `let foo = Foo()`

I find some way to let this code work in RELEASE mode.

Add `print(animal)` before end of f1 scope.

or

Remove switch case, use `let animal: Animal = Dog()`

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

No branches or pull requests

1 participant