I’m porting some C to Swift and I need to pass a Swift instance through a `void *` (ie, UnsafeMutableRawPointer). It works, in one thread, but when a second thread accesses the exact same pointer, it fails with memory errors like EXC_BAD_ACCESS
The code below could be pasted into an AppDelegate.swift in a new single view iOS project.
Anyone know what’s going on here? The second call to `tryUsingUnsafePointer`, in the new thread, crashes.
The problem is that the main thread is deallocating the object before the other thread has a chance to see it. Ideally you'd pass a managed object reference to the block that runs in the other thread so the reference count does not fall to zero. If you absolutely need to pass it as an unsafe pointer then you must manage the reference count manually, like this:
let unsafePtr = Unmanaged.passRetained(self).toOpaque()
let safe = Unmanaged<PointerTest>.fromOpaque(unsafePtr).takeRetainedValue()
useSafeObject(safe)
Make sure the calls to passRetained and takeRetainedValue are balanced. (If this is a callback that gets called multiple times, use takeRetainedValue only once at the end.)
···
Le 18 juin 2017 à 9:23, Robert Nikander via swift-users <swift-users@swift.org> a écrit :
Hi,
I’m porting some C to Swift and I need to pass a Swift instance through a `void *` (ie, UnsafeMutableRawPointer). It works, in one thread, but when a second thread accesses the exact same pointer, it fails with memory errors like EXC_BAD_ACCESS
The code below could be pasted into an AppDelegate.swift in a new single view iOS project.
Anyone know what’s going on here? The second call to `tryUsingUnsafePointer`, in the new thread, crashes.
The main thread isn’t deallocating the PointerTest object, because it’s saved in the AppDelegate.
Rob
···
On Jun 18, 2017, at 10:45 AM, Michel Fortin <michel.fortin@michelf.ca> wrote:
The problem is that the main thread is deallocating the object before the other thread has a chance to see it. Ideally you'd pass a managed object reference to the block that runs in the other thread so the reference count does not fall to zero. If you absolutely need to pass it as an unsafe pointer then you must manage the reference count manually, like this:
let unsafePtr = Unmanaged.passRetained(self).toOpaque()
let safe = Unmanaged<PointerTest>.fromOpaque(unsafePtr).takeRetainedValue()
useSafeObject(safe)
Make sure the calls to passRetained and takeRetainedValue are balanced. (If this is a callback that gets called multiple times, use takeRetainedValue only once at the end.)
Le 18 juin 2017 à 9:23, Robert Nikander via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> a écrit :
Hi,
I’m porting some C to Swift and I need to pass a Swift instance through a `void *` (ie, UnsafeMutableRawPointer). It works, in one thread, but when a second thread accesses the exact same pointer, it fails with memory errors like EXC_BAD_ACCESS
The code below could be pasted into an AppDelegate.swift in a new single view iOS project.
Anyone know what’s going on here? The second call to `tryUsingUnsafePointer`, in the new thread, crashes.
Oh, right, my mistake. I think the problem is that UnsafeMutableRawPointer(&mSelf) creates a pointer to the local variable mSelf on the stack. When mSelf goes out of scope, your raw pointer to an object reference pointer on the stack becomes invalid. (And it could become invalid even earlier with optimisations).
The correct way to pass your pointer is the one I described earlier. You can use passUnretained and takeUnretainedValue if you want to avoid touching the retain count since the AppDelegate will retain the object anyway.
···
Le 18 juin 2017 à 11:21, Robert Nikander via swift-users <swift-users@swift.org> a écrit :
The main thread isn’t deallocating the PointerTest object, because it’s saved in the AppDelegate.
Rob
On Jun 18, 2017, at 10:45 AM, Michel Fortin <michel.fortin@michelf.ca <mailto:michel.fortin@michelf.ca>> wrote:
The problem is that the main thread is deallocating the object before the other thread has a chance to see it. Ideally you'd pass a managed object reference to the block that runs in the other thread so the reference count does not fall to zero. If you absolutely need to pass it as an unsafe pointer then you must manage the reference count manually, like this:
let unsafePtr = Unmanaged.passRetained(self).toOpaque()
let safe = Unmanaged<PointerTest>.fromOpaque(unsafePtr).takeRetainedValue()
useSafeObject(safe)
Make sure the calls to passRetained and takeRetainedValue are balanced. (If this is a callback that gets called multiple times, use takeRetainedValue only once at the end.)
Le 18 juin 2017 à 9:23, Robert Nikander via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> a écrit :
Hi,
I’m porting some C to Swift and I need to pass a Swift instance through a `void *` (ie, UnsafeMutableRawPointer). It works, in one thread, but when a second thread accesses the exact same pointer, it fails with memory errors like EXC_BAD_ACCESS
The code below could be pasted into an AppDelegate.swift in a new single view iOS project.
Anyone know what’s going on here? The second call to `tryUsingUnsafePointer`, in the new thread, crashes.