CoreAudio custom property memory leak

I'm having issues with different ARC behaviour on different macOS setups while developing an Audio Server PlugIn.

I'm using the kAudioObjectPropertyCustomPropertyInfoList property to declare my custom properties and one of them is declared as kAudioServerPlugInCustomPropertyDataTypeCFPropertyList to be able to pass CFDataRef containing arbitrary bytes. I haven't found any documentation regarding the memory management in this case, but I noticed that when I receive new data via AudioObjectSetPropertyData it leaks memory so I added CFRelease once I'm done with the data object - the memory leak was gone and I settled on an assumption that the CoreAudio helper and/or daemon does not do any automatic releases of incoming data and it's my responsibility to release it.

But then I gave the plugin away for testing to my colleague who had a MacBook Pro (2014) running macOS Big Sur 11.3.1 and the driver started crashing with:

Thread 3 Crashed:: Dispatch queue: com.apple.NSXPCConnection.user.anonymous.81372
0   libobjc.A.dylib                 0x00007fff203844af objc_release + 31
1   libobjc.A.dylib                 0x00007fff203a220f AutoreleasePoolPage::releaseUntil(objc_object**) + 167
2   libobjc.A.dylib                 0x00007fff20384e30 objc_autoreleasePoolPop + 161
3   libxpc.dylib                    0x00007fff2022ac24 _xpc_connection_call_event_handler + 56
4   libxpc.dylib                    0x00007fff20229a9b _xpc_connection_mach_event + 938
5   libdispatch.dylib               0x00007fff2033a886 _dispatch_client_callout4 + 9
6   libdispatch.dylib               0x00007fff20351aa0 _dispatch_mach_msg_invoke + 444
7   libdispatch.dylib               0x00007fff20340473 _dispatch_lane_serial_drain + 263
8   libdispatch.dylib               0x00007fff203525e2 _dispatch_mach_invoke + 484
9   libdispatch.dylib               0x00007fff20340473 _dispatch_lane_serial_drain + 263
10  libdispatch.dylib               0x00007fff203410c0 _dispatch_lane_invoke + 417
11  libdispatch.dylib               0x00007fff2034abed _dispatch_workloop_worker_thread + 811
12  libsystem_pthread.dylib         0x00007fff204e14c0 _pthread_wqthread + 314
13  libsystem_pthread.dylib         0x00007fff204e0493 start_wqthread + 15

That was hinting of possible double release and usage of autoreleasepool. Once I removed the CFRelease the driver stopped crashing and no memory leaks were happening on his mac (while they returned on mine).

So my question is - who's responsiblity it is to release the incoming CFDataRef (and probably also CFStringRef) data in CoreAudio property setters?

If that's not certain, then is it possible to detect if my plugin is called under ARC or not (I can't find any APIs to query autorelease pool stack - there's just push and pop)?

PS. Machines that I tested this on are iMac (2017) and MacBook Air (2020) both running macOS Monterey 12.5.1

Replies

There is also something weird happening in the Core Audio Server Plug-In sandbox process itself - it seems to leak memory with custom properties on macOS 12.5.1. It seems related to the issue mentioned above, although if I do release the CFDataRef Instruments still complains about the leak in +[NSPropertyListSerialization propertyListWithData:options:format:error:]