Simultaneous memory access warning in Xcode 9 beta

Hi guys,

I’m writing an OpenGL ES app and from Xcode 9 beta 3 onwards I am getting this runtime error for every use of withUnsafePointer withMemoryRebound:

Simultaneous accesses to 0x7fe2d082ae80, but modification requires exclusive access.
Previous access (a modification) started at Leapfrog Viewer`SceneViewController.glkView(_:drawIn:) + 2034 (0x106627a72).
Current access (a read) started at:
0    libswiftCore.dylib                 0x000000010b0d70e0 swift_beginAccess + 505
1    Leapfrog Viewer                    0x000000010661f670 SceneViewController.normalMatrix.getter + 79
2    Leapfrog Viewer                    0x0000000106628f40 closure #3 in SceneViewController.glkView(_:drawIn:) + 88
3    Leapfrog Viewer                    0x00000001066290e0 partial apply for closure #3 in SceneViewController.glkView(_:drawIn:) + 58
... <edited>
19   UIKit                              0x0000000108d3e8af UIApplicationMain + 159
20   Leapfrog Viewer                    0x00000001066a0ad0 main + 55
21   libdyld.dylib                      0x000000010ca6d3c4 start + 1

Xcode highlights this line:

withUnsafePointer(to: &normalMatrix) {
        $0.withMemoryRebound(to: GLfloat.self, capacity: MemoryLayout.size(ofValue: normalMatrix)) {
            glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, GLboolean(GL_FALSE), $0)
        }
}

I know that Swift 4 is bringing in protections for simultaneous accesses of memory in a lot of situations, but I thought that Unsafe Pointers were being left to the dev to handle properly?
If this warning really is due to my own fault, how do I go about rewriting it to avoid the warning?
Thanks.

Hey @knight556!

What line is the crash occurring? Is this happening because you are accessing normalMatrix when working out the capacity while accessing the unsafe pointer itself?

I might be completely off with that though :sweat_smile:

Hey, thanks for the reply.
I should have given more information in my question, but it already seemed long enough.
The app isn’t actually crashing, Xcode is just displaying the same runtime error over and over again, making the app unusable in the simulator.
The identical error is shown for the modelview and modelviewprojection matrices as well.
I’m hoping that Apple is still working out the kinks with the new memory access rules in Swift 4 and this error might disappear one day… but that’s not too likely.

Ahh I see! I was reading over the Swift Evolution proposal about this that is going into Swift 4, I think you might be right about still having to manage it yourself if you use unsafe pointers but who knows with Xcode betas :sweat_smile:

I’d be interested to know how you get on with this though if you do find a solution :slightly_smiling_face: Good luck!

I managed to get rid of the runtime warning but specifically capturing the variable in the closure (even though this wasn’t required before Swift 4). I might have gotten myself into a strong reference loop by capturing self.

withUnsafePointer(to: &normalMatrix) { **[normalMatrix] in**
    $0.withMemoryRebound(to: GLfloat.self, capacity: MemoryLayout.size(ofValue: normalMatrix)) {
        glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, GLboolean(GL_FALSE), $0)
    }
}

Ahh glad you finally sorted the issue in the end :raised_hands: