[swift-users] Simultaneous accesses, but modification requires exclusive access

somu subscribe somu.subscribe at gmail.com
Mon Jul 24 01:04:07 CDT 2017


Hi,

I am having trouble with the below mentioned code.

It crashes in Xcode 9.0 beta 3 (9M174d) but doesn’t crash in Xcode 8.3.3 (8E3004b)

Observations in Xcode 9:
- When Car is changed into a struct and test is made to be mutating, it doesn’t crash
- When Helper is changed to class, it doesn’t crash
- In Helper, when doSomething is changed to be non-mutating, it doesn’t crash

Questions:
- Is there a bug in my code which is being detected in Xcode 9 ? If so could you please explain and suggest an alternate approach / fix ?
- Is this occurring because class doesn’t have the concept of mutating but struct does and passing one’s function into the other causes this ?

Code:

struct Helper {
    
    var v1 = 10
    
    mutating func doSomething(f1: () -> ()) {
        f1()
    }
}

class Car {
    var helper = Helper()
    
    func test() {
        helper.doSomething(f1: f1)
    }
    
    func f1() {
        _ = helper.v1 //Crash - Simultaneous accesses to <memory address>, but modification requires exclusive access.
    }
}

var car = Car()
car.test()


Crash:

Simultaneous accesses to <memory address1>, but modification requires exclusive access.
Previous access (a modification) started at SimultaneousAccess`Car.test() + 65 (<memory address2>).

Testing:
- Tested by creating macOS command line application and iOS application (Simulator)

Thanks and regards,
Muthu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170724/56576e1a/attachment.html>


More information about the swift-users mailing list