Deinit method is not called in Xcode 10 Beta 6 playground

前端 未结 1 660
时光取名叫无心
时光取名叫无心 2021-01-25 01:32

I was exploring memory management concept and found deinit method is not calling in Xcode 10 beta 6 playground. Initially I thought may be some pro

相关标签:
1条回答
  • 2021-01-25 02:14

    This issue seems to still be present in Xcode 10.0 (10A255). I have playground with the following code:

    class Person {
        var name: String
    
        func buy(car: Car) {
            car.owner = self
        }
    
        init(name: String) {
            self.name = name
            print("Person \(name) is initalized")
        }
    
        deinit {
            print("Person \(name) is being deinitialized")
        }
    }
    
    
    class Car {
        let model: String
        weak var owner: Person?
    
        init(model: String) {
            self.model = model
            print("Car \(model) is initialized")
        }
    
        deinit {
            print("Car \(model) is being deinitialized")
        }
    }
    
    print("===== before do =====")
    do {
        print("    ---- do begin -----")
        let johnny = Person(name: "John")
        let porsche = Car(model: "Carrera4")
        johnny.buy(car: porsche)
        print("    ---- do end -----")
    }
    print("===== after do =====")
    

    In Xcode 9.4.1 both Car's and Person's deinit are executed, but in Xcode 10.0 (10A255) the Person's deinit method is not executed.

    The same code inside a test macOS project works as expected (both deinit executed) in Xcode 9.4.1 as well as Xcode 10.0 (10A255)

    0 讨论(0)
提交回复
热议问题