Description
Apple platform
iOS
Framework version
net8.0-*
Affected platform version
VS2022 17.13.6
Description
Our app makes use of Bluetooth LE, and continuously attempts to connect to a local device when the user has configured it. If the user forgets to turn the device on, or their device is not with them, intended behaviour is to keep trying to connect to it so that when it becomes available, the connection succeeds.
Every time we attempt to make a connection, we get a new or existing CBPeripheral from CBCentralManager.RetrievePeripheralsWithIdentifiers, hook its events, then try to connect to it. After a given timeout, we clean everything up, including unhooking the events before calling CBCentralManager.CancelPeripheralConnection. Then after a short interval the connection process is started again fresh.
We are seeing that after trying to do this over 200 attempts, the runtime crashes with the stack trace below.
ObjCRuntime.RuntimeException: Failed to marshal the Objective-C object 0x114616380 (type: CoreBluetooth_CBPeripheral__CBPeripheralDelegate). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'CoreBluetooth.CBPeripheral+_CBPeripheralDelegate' does not have a constructor that takes one NativeHandle argument).
at ObjCRuntime.Runtime.MissingCtor(IntPtr , IntPtr , Type , MissingCtorResolution , IntPtr , RuntimeMethodHandle )
at ObjCRuntime.Runtime.ConstructNSObject[NSObject](IntPtr , Type , MissingCtorResolution , IntPtr , RuntimeMethodHandle )
at ObjCRuntime.Runtime.ConstructNSObject[NSObject](IntPtr , Type , MissingCtorResolution )
at ObjCRuntime.Runtime.ConstructNSObject(IntPtr , IntPtr , MissingCtorResolution )
at ObjCRuntime.Runtime.GetNSObject(IntPtr , MissingCtorResolution , Boolean )
at ObjCRuntime.Runtime.GetNSObject(NativeHandle )
at CoreBluetooth.CBPeripheral.EnsureCBPeripheralDelegate()
at CoreBluetooth.CBPeripheral.add_DiscoveredService(EventHandler`1 )
As you can see, we are using the built-in events on CBPeripheral rather than attaching our own delegate to it. Perhaps this has something to do with the exception. I would like to know under what circumstances you'd expect to see this exception.
Steps to Reproduce
I do not have a simple repro project. While I might be able to create one, I would rather someone looked at the stack trace and gave me some preliminary advice if possible.
Did you find any workaround?
No response
Relevant logs
No response