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-6003] EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when accessing dictionary keys #48560

Closed
swift-ci opened this issue Sep 26, 2017 · 3 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6003
Radar rdar://problem/34672149
Original Reporter sega-zero (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Had this on the latest Xcode 8, now catching this on Xcode 9.0 (9A235). Both swift 3 and swift 4.

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @lorentey
Priority Medium

md5: e30e1458ce0718cb97075850ff042262

is duplicated by:

  • SR-7305 Error when accessing .keys or .values on [String: Int]

Issue Description:

Having some concurrent code in my project and noticed that sometimes printing a dictionary keys may lead to crash. Tried to reproduce this bug and came to this code snippet:

class ViewController: UIViewController {
    struct Test {
        let value: Int
    }

    private let syncQueue = DispatchQueue(label: "sync queue")
    private var test = [String: Test](minimumCapacity: 4) {
        didSet {
            self.syncQueue.async {
                print("setting from \(oldValue)")
                let values = Array(self.test.values.map { $0.value })
                print(values)
            }
        }
    }

    var lastKey = ""
    private var timer1: Timer!

    override func viewDidLoad() {
        super.viewDidLoad()

        timer1 = Timer(timeInterval: 1, repeats: true) { (timer) -> Void in
            self.syncQueue.async {
                self.test[self.lastKey] = nil
                print("active: \(self.test.keys)")// crash on this line
            }
        }
        RunLoop.current.add(timer1, forMode: RunLoopMode.defaultRunLoopMode)

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
            self.syncQueue.async {
                self.test["one"] = Test(value: 0)
            }
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 4) {
            self.syncQueue.async {
                self.test["two"] = Test(value: 0)
            }
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
            self.lastKey = "two"
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 6) {
            self.lastKey = "one"
        }
    }
}

When running this on iPhone 6s+ simulator, this code will crash in 100 cases out of 100.

If I change the code to

print("active: \(self.test.keys.map { $0 })")

then no crash is occured.

@belkadan
Copy link
Contributor

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Christopher Barnes (JIRA)

I may have also run into this.

I have an iOS app where I'm instantiating a Dictionary [String:CustomObject] in the main thread, and then adding to it from an Operation.

At first I thought I was setting the keys inappropriately because at compile time it wouldn't let me access the keys variable, but then when I tried to print the keys during runtime it would crash.

Using the workaround above, the code now appears to work.

@lorentey
Copy link
Member

snowstorms (JIRA User): That indeed sounds like the same bug.

The fix for this has been merged to master and swift-4.1-branch; you can download a recent snapshot from swift.org/download to verify if it resolves your issue.

@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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants