0

I have a swift app where I use the native C++ lib and there is a method that takes as an argument void * on MTLTexture

void RenderAPI_Metal::EndModifyTexture(void* textureHandle)
{
    id<MTLTexture> tex = (__bridge id<MTLTexture>)textureHandle;
...
}

and Swift call is

func foo(texture: MTLTexture) {
...

  EndModifyTexture(&texture)
...
}

So, on the Swift side, I call the method and pass a pointer, and then on the C++ side when I try to cast it back I got an error

om.apple.scenekit.scnview-renderer (20): EXC_BAD_ACCESS (code=1, address=0x0)

So, according to the error looks like the pointer is nil, however, when I check it in the debug I see that it has an address void* textureHandle is 0x0000000280a08178

What is the problem here? Why did I pass MTLTexture and then I got a problem casting it back?

P.S.

I can't change the implementation, C++ method should receive void*

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • 2
    Possibly helpful: https://stackoverflow.com/q/33294620/1187415. – Martin R Aug 29 '21 at 12:16
  • This looks very close to [your previous question](https://stackoverflow.com/questions/68972527/how-to-get-unsaferawpointer-on-the-swift-object), you should not create many questions about the same problem but edit existing question with new details – Phil Dukhov Aug 29 '21 at 12:29
  • @PhilipDukhov, but why do you think it is close to the previous question? The previous one is about the proper way to get a pointer on a certain swift object and this one is about how to cast the object back... I didn't get your suspicion. – Sirop4ik Aug 29 '21 at 12:35

1 Answers1

0

Thanks to @MartinR I found the solution here

https://stackoverflow.com/a/33310021/5709159

exactly this method helps me to solve the problem

func bridgeRetained<T : AnyObject>(obj : T) -> UnsafeRawPointer {
    return UnsafeRawPointer(Unmanaged.passRetained(obj).toOpaque())
}
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121