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-5289] unowned NSObject property is crashing with EXC_BAD_ACCESS #47864

Closed
swift-ci opened this issue Jun 23, 2017 · 12 comments
Closed

[SR-5289] unowned NSObject property is crashing with EXC_BAD_ACCESS #47864

swift-ci opened this issue Jun 23, 2017 · 12 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5289
Radar rdar://problem/32985961
Original Reporter GrzegorzLeszek (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee @tbkka
Priority Medium

md5: 6ad2d49f295da6aa8bb9e1ee602bb0dd

relates to:

  • SR-5310 unowned object crashes in noescape closures

Issue Description:

A concise description of the problem:
EXC_BAD_ACCESS when unowned property is NSObject

A reproducible test case:

import Foundation

class Bar: NSObject {
}

class Foo {
    unowned let bar: Bar // crash is gone if: Bar is not NSObject class OR bar is a weak optional variable property
    init(bar: Bar) {
        self.bar = bar
    }
}

let bar = Bar()
let foo = Foo(bar: bar) // EXC_BAD_ACCESS

A description of the environment that reproduces the problem:
Consistent crash on Xcode 8.3.2 (Swift 3.1) – MacOS Sierra 10.12.4
Tested also with Swift Toolchain 4.0 (Snapshot 2017-05-23 a)
Reproducible with playground
Possible similar issue: SR-1006

@belkadan
Copy link
Contributor

I'm not seeing this with Xcode 9 beta 1. Mind checking if it's still showing up for you?

@swift-ci
Copy link
Collaborator Author

Comment by Grzegorz Leszek (JIRA)

Tested with Playground:
Xcode 9 beta (9M136h)
Xcode 9 beta 2 (9M137d)
Both are reporting same error ( in attachment )

@belkadan
Copy link
Contributor

Hm. Maybe it only happens in playgrounds, or maybe it's target-dependent. Is this a macOS, iOS, or tvOS playground?

@belkadan
Copy link
Contributor

@swift-ci create

@gparker42
Copy link
Mannequin

gparker42 mannequin commented Jun 26, 2017

Yes, it's a playground-only crash. Looks like the mirror machinery is not correctly handling the special cases in unowned variables.

@swift-ci
Copy link
Collaborator Author

Comment by Grzegorz Leszek (JIRA)

It is not playground only. It is target-dependent. I've managed to add example project. Tested on Xcode 8.3.2 and Xcode 9 beta 1. Crash happens after accessing the property.

macOS iOS tvOS
OK crash crash

@gparker42
Copy link
Mannequin

gparker42 mannequin commented Jun 27, 2017

Grzegorz, are you seeing this on iOS/tvOS device or simulator or both?

@swift-ci
Copy link
Collaborator Author

Comment by Grzegorz Leszek (JIRA)

Both iOS device (10.3.2) and iOS simulator (10.3).
Same crash on tvOS. I did not test tv device.
Tested on Xcode 8.3.2 (8E2002), macOS Sierra

@gparker42
Copy link
Mannequin

gparker42 mannequin commented Jun 30, 2017

Thanks, that reproduces everywhere else. The final print() is required when not running in a playground; a playground implicitly prints the result which causes the same crash.

This is a bug in loadSpecialReferenceStorage() in swift_ClassMirror_subscript(). It assumes that any variable that is not weak is strong. This is incorrect for unowned variables. The crash occurs because an unowned variable pointing to an ObjC object does not simply store a pointer to the object.

@tbkka
Copy link
Contributor

tbkka commented Nov 19, 2019

I believe this is fixed by PR #28368

@tbkka
Copy link
Contributor

tbkka commented Mar 4, 2020

PR#28368 should have resolved this. Please let me know if it's still happening for you.

@swift-ci
Copy link
Collaborator Author

Comment by Grzegorz Leszek (JIRA)

Hi @tbkka, It is PARTIALLY fixed. It does not work in the playgrounds.

✅ iOS device and iOS simulator

❌ playground macOS Catalina, Xcode 11

Error Message:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

Nevertheless, current fix is great and really helpful in iOS development, where I still need to work with Obj-C objects.
Now I can use unowned keyword more often �.
Thanks!

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

No branches or pull requests

3 participants