access violation when weak variable


(Ray Fix) #1

Hi!

When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a access violation. I think this is a bug, but want to make sure I am not missing something.

Best regards,
Ray

//: Playground - noun: a place where people can play

import UIKit

class Person: CustomStringConvertible {
    var name: String
    weak var parent: Person? /// If I remove weak, no crash in Xcode 8 beta 2, but leaks
    var children: [Person] = [] {
        didSet {
            children.forEach { $0.parent = self }
        }
    }
    
    init(name: String) {
        self.name = name
        print("initialized \(name)")
    }
    deinit {
        print("deinit \(name)")
    }
    var description: String {
        return name
    }
}

do {
    let frank = Person(name: "Frank")
    let lisa = Person(name: "Lisa")
    frank.children = [lisa] /// KABOOM!
}


(Mark Dalrymple) #2

The actual death is happening when setting the parent:

do {
    let frank = Person(name: "Frank")
    print("frank\(frank)")
    let lisa = Person(name: "Lisa")
    frank.parent = lisa // Dies here with EXC_BAD_ACCESS

You can click the eye to get a stack trace. (not included here because of
rdar://27263098 - can't get a copyable stack trace from a hard error in
playgrounds).

My bet it's something playground related - about 9 frames down in the stack
trace has a reference to the Playground Logger doing the Mirror thing.

Putting your code in to a standalone swift executable lets it work.

Cheers,
++md

···

On Sat, Jul 9, 2016 at 10:18 AM, Ray Fix via swift-users < swift-users@swift.org> wrote:

Hi!

When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a
access violation. I think this is a bug, but want to make sure I am not
missing something.

Best regards,
Ray

//: Playground - noun: a place where people can play

import UIKit

class Person: CustomStringConvertible {
    var name: String
    weak var parent: Person? /// If I remove weak, no crash in Xcode 8
beta 2, but leaks
    var children: [Person] = [] {
        didSet {
            children.forEach { $0.parent = self }
        }
    }

    init(name: String) {
        self.name = name
        print("initialized \(name)")
    }
    deinit {
        print("deinit \(name)")
    }
    var description: String {
        return name
    }
}

do {
    let frank = Person(name: "Frank")
    let lisa = Person(name: "Lisa")
    frank.children = [lisa] /// KABOOM!
}

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #3

You code works fine in Xcode 7.3.1 (7D1014). So it must be a bug in Xcode 8.

Zhaoxin

···

On Sat, Jul 9, 2016 at 10:18 PM, Ray Fix via swift-users < swift-users@swift.org> wrote:

Hi!

When I make a variable weak in Xcode 8 (both beta 1 and beta 2) I get a
access violation. I think this is a bug, but want to make sure I am not
missing something.

Best regards,
Ray

//: Playground - noun: a place where people can play

import UIKit

class Person: CustomStringConvertible {
    var name: String
    weak var parent: Person? /// If I remove weak, no crash in Xcode 8
beta 2, but leaks
    var children: [Person] = [] {
        didSet {
            children.forEach { $0.parent = self }
        }
    }

    init(name: String) {
        self.name = name
        print("initialized \(name)")
    }
    deinit {
        print("deinit \(name)")
    }
    var description: String {
        return name
    }
}

do {
    let frank = Person(name: "Frank")
    let lisa = Person(name: "Lisa")
    frank.children = [lisa] /// KABOOM!
}

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users