Unsafe unretained & callbacks from audio thread


On mobile, apologies for formatting or misses names

My app has a timing listener which runs on the audio thread. At the end of this callback, I want to notify my obj-c object that something happened, so I have a struct with an __unsafe__unretained pointer to the object on my c callback, and I pass this struct in the block sent to performOnMainThread.

This block does run on the correct thread by sometimes crashes as the obj-c object has already been destroyed and the unsafe pointer is now invalid.

My question is: how my c timing callback ensure that it won't send a message to an obj-c object that has been destroyed?



  • The best way to do this is use CFBridgingRetain() when assigning the pointer, and then CFBridgingRelease() once the message is received on the main thread. (you'll need to use a bridging cast to/from CFTypeRef here and there to make that work)

Sign In or Register to comment.