xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.48k stars 514 forks source link

CoreBluetooth.CBPeripheral+_CBPeripheralDelegate does not have a constructor that takes one IntPtr argument #4207

Closed MKuckert closed 6 years ago

MKuckert commented 6 years ago

Steps to Reproduce

  1. Use the CBCentralManager to discover Bluetooth Low Energy devices and register an event listener for the DiscoveredPeripheral event.
  2. Attach any event listener (e.g. for the DiscoveredService event) on the discovered CBPeripheral instance (reached into the listener using the event args Peripheral property).

Expected Behavior

The event listener for the DiscoveredService event should be successfully registered.

Actual Behavior

In some rare cases an exception is thrown:

System.Exception: Failed to marshal the Objective-C object 0x1cc02a440 (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 IntPtr argument).
at ObjCRuntime.Runtime.MissingCtor (System.IntPtr ptr, System.IntPtr klass, System.Type type, ObjCRuntime.Runtime+MissingCtorResolution resolution) <0x10314cc9c + 0x00104> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at ObjCRuntime.Runtime.ConstructNSObject[T] (System.IntPtr ptr, System.Type type, ObjCRuntime.Runtime+MissingCtorResolution missingCtorResolution) <0x10314ce28 + 0x00063> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at ObjCRuntime.Runtime.ConstructNSObject (System.IntPtr ptr, System.IntPtr klass, ObjCRuntime.Runtime+MissingCtorResolution missingCtorResolution) <0x10314cda4 + 0x0004b> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at ObjCRuntime.Runtime.GetNSObject (System.IntPtr ptr, ObjCRuntime.Runtime+MissingCtorResolution missingCtorResolution, System.Boolean evenInFinalizerQueue) <0x10314d538 + 0x00063> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at CoreBluetooth.CBPeripheral.get_WeakDelegate () <0x1031241b4 + 0x0002b> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at CoreBluetooth.CBPeripheral.get_Delegate () <0x103124040 + 0x0000f> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at CoreBluetooth.CBPeripheral.EnsureCBPeripheralDelegate () <0x10312435c + 0x00013> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at CoreBluetooth.CBPeripheral.add_DiscoveredService (System.EventHandler`1[TEventArgs] value) <0x103124500 + 0x00013> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at HAL.iOS.BlueToothLE.Device..ctor (CoreBluetooth.CBPeripheral peripheral, System.Int32 rssi, System.Byte[] scanRecord) <0x1039f07f0 + 0x001fb> in <d42285d588044ff3ba5304ce63ff5e83#645e90724b9e815ff25536c8f141d728>:0 
  at HAL.iOS.BlueToothLE.CentralAdapter._central_DiscoveredPeripheral (System.Object sender, CoreBluetooth.CBDiscoveredPeripheralEventArgs e) <0x1039ef158 + 0x001cf> in <d42285d588044ff3ba5304ce63ff5e83#645e90724b9e815ff25536c8f141d728>:0 
  at CoreBluetooth.CBCentralManager+_CBCentralManagerDelegate.DiscoveredPeripheral (CoreBluetooth.CBCentralManager central, CoreBluetooth.CBPeripheral peripheral, Foundation.NSDictionary advertisementData, Foundation.NSNumber RSSI) <0x103123aa8 + 0x0008f> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x10313a6e0 + 0x0009b> in <b7935acd70e343049845d6fd73e5ec44#645e90724b9e815ff25536c8f141d728>:0 
  at Hoermann.BleApp.iOS.Program.Main (System.String[] args) <0x1026642f8 + 0x0001b> in <afda903f5ef043fd93ca67f575fe33f0#645e90724b9e815ff25536c8f141d728>:0

The _CBPeripheralDelegate has indeed no constructor that takes one IntPtr argument.

Environment

Observed on an iPhone 8 (A1905) running iOS 11.3.1

=== Visual Studio Community 2017 for Mac ===

Version 7.4 (build 1033)
Installation UUID: 9ab03795-d9e6-47fd-b0f9-e24f86856cc5
Runtime:
    Mono 5.10.0.153 (2017-12/78136c4a903) (64-bit)
    GTK+ 2.24.23 (Raleigh theme)

    Package version: 510000153

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    2.0.5
    2.0.3
    2.0.0
SDK: /usr/local/share/dotnet/sdk/2.1.4/Sdks
SDK Versions:
    2.1.4
    2.0.3
    2.0.0
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.10.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.1
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 9.1 (13532)
Build 9B55

=== Xamarin.Mac ===

Version: 4.2.0.20 (Visual Studio Community)

=== Xamarin.iOS ===

Version: 11.8.0.20 (Visual Studio Community)
Hash: 1c6f300e
Branch: d15-6
Build date: 2018-02-07 23:42:40-0500

=== Xamarin.Android ===

Version: 8.2.0.15 (Visual Studio Community)
Android SDK: /Users/ciuser/Library/Android/sdk
    Supported Android versions:
        5.1 (API level 22)
        7.0 (API level 24)
        7.1 (API level 25)
        8.0 (API level 26)
        8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 27.0.1
SDK Build Tools Version: 27.0.3

Java SDK: /usr
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Inspector ===

Version: 1.4.0
Hash: b3f92f9
Branch: master
Build date: Fri, 19 Jan 2018 22:00:34 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 704001033
Git revision: 053991068f3c88497d3e5cc52fe7fdf57c28b62f
Build date: 2018-02-28 05:02:28-05
Xamarin addins: cc3283953e0af0b8bfab1bdfdf847d9e403c4606
Build lane: monodevelop-lion-d15-6

=== Operating System ===

Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0
    Tue Jan 30 11:27:06 PST 2018
    root:xnu-3789.73.11~1/RELEASE_X86_64 x86_64

Build Logs

https://gist.github.com/MKuckert/274c50f61fe648b4d3368a9a7959a6d5

Example Project (If Possible)

Not possible

rolfbjarne commented 6 years ago

This sounds like https://bugzilla.xamarin.com/show_bug.cgi?id=34242. Can you try the workaround at the end of the comment her: https://bugzilla.xamarin.com/show_bug.cgi?id=34242#c9 and see if that works for you?

rolfbjarne commented 6 years ago

We have not received the requested information. If you are still experiencing this issue please provide all the requested information then click the Reopen Issue button. Thanks!

MKuckert commented 6 years ago

Just for completeness: we've a workaround in the field but experience crashes and are currently reviving our code for implementation errors

SprengerS commented 6 years ago

We also exeperiencing this problem. Our system is discovering every 150ms a CBPeripheral from 1 of 10 Devices. When you save every CBPeripheral in a static List during a 10 s scan intervall you will have up to 1000 CBPeripheral objects. This is a massive Memory Leak. So I clear this static List after everey scan intervall, but this crash also happens. So saving all peripheral objects in a static List will not fix this problem. We just need a bugfix not a workaround

sichy commented 5 years ago

So where did this land? Why there is not a fix already just adding the missing IntPtr constructor to Xamarin.IOS bindings? we are hitting the exactly same issue too.

SprengerS commented 5 years ago

Hi i have implemented the ICBPeripheralDelegate explicitly on my own. It inherits NSObject so that the IntPtr Constructor is implemented. The Delegates will be forwarded to my own Events