Playgrounds holding on to weak reference unnecessarily

Originator:jared.sorge
Number:rdar://47150688 Date Originated:1/9/2019
Status:closed Resolved:
Product:Developer Tools Product Version:10.1
Classification:dupe Reproducible:
 
Summary: It seems that playgrounds hold on to weak instances unnecessarily. Attached is a sample playground to demonstrate what I'm talking about.

If you copy the `Weak` enum definition and do this in a sample project, it deallocates properly.

Contents of the playground:

```
import Foundation

enum Weak<T> {
    class Box {
        weak var object: AnyObject?
        init(_ object: AnyObject) { self.object = object }
    }

    case reference(Box)
    case value(T)

    init(_ instance: T) {
        if type(of: instance) is AnyClass {
            self = .reference(Box(instance as AnyObject))
        } else {
            self = .value(instance)
        }
    }

    var instance: T? {
        switch self {
        case .reference(let box):
            return box.object as? T
        case .value(let instance):
            return instance
        }
    }
}

class A {}
let two = Weak(A())
two.instance   // should be `nil`, but is `A`
```

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!