locked
Failed to marshal the Objective-C object... RRS feed

  • Question

  • User62728 posted

    I'm seeing this crash consistently at the moment. I've seen it occur from a few different types of objects. From searches, this seems to be related to objects being GC'd and then referenced. Not sure what I'm doing to bring on the issue and I've seen one reference to this being a bug in the GC itself.

    In my case I started with a single view application and then removed the additional controllers to get down to a single rootviewcontroller.cs. I'm accessing this.Window.RootViewController in FinishedLaunching. I do the same in a Swift app with no issue -- I'm in the middle of making a port from a Swift app into a Xamarin equivalent app.

    C#

        public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
        {
            // Override point for customization after application launch.
    
            beaconManager = new BeaconManager (this.Window.RootViewController as BeaconView);
    
            return true;
        }
    

    Swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    
        self.ibeaconManager = iBeaconManager(beaconView: self.window?.rootViewController as BeaconView)
    
        return true
    }
    

    System.Exception: Failed to marshal the Objective-C object 0x15e54790 (type: RootViewController). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'ProximlyXamarin.RootViewController' does not have a constructor that takes one IntPtr argument). at MonoTouch.ObjCRuntime.Runtime.MissingCtor (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) [0x00046] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/.pmcs-compat.Runtime.cs:400 at MonoTouch.ObjCRuntime.Runtime.ConstructNSObject[UIViewController] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in :0 at MonoTouch.ObjCRuntime.Runtime.GetNSObject[UIViewController] (IntPtr ptr) [0x00000] in :0 at MonoTouch.UIKit.UIWindow.get_RootViewController () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:282 at ProximlyXamarin.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication application, MonoTouch.Foundation.NSDictionary launchOptions) [0x00008] in /Users/jay/Projects/ProximlyXamarin/ProximlyXamarin/AppDelegate.cs:27 at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 at ProximlyXamarin.Application.Main (System.String[] args) [0x00008] in /Users/jay/Projects/ProximlyXamarin/ProximlyXamarin/Main.cs:17

    Wednesday, July 9, 2014 2:26 AM

All replies

  • User181 posted

    Check you .xib or storyboard file. I think you may have set the custom class of your root view controller to "RootViewController". It looks like you expect it to be BeaconView (which I hope is a UIViewController subclass and not a UIView subclass). You should also make sure that you have a [Register("BeaconView")] attribute attached to BeaconView so that the runtime knows the Objective-C name of that class.

    Wednesday, July 9, 2014 3:23 AM
  • User62728 posted

    BeaconView is an interface, defined as:

    ``` public interface BeaconView { bool lookingForBeacon { get; set; }

        void showImage (string name);
    
        void notify (string message);
    }
    

    public partial class RootViewController : UIViewController, BeaconView { ... } ```

    my Swift equivalent is:

    ``` @objc protocol BeaconView { var lookingForBeacon: Bool { get set } func showImage(name: String) func notify(message: String) }

    class ViewController: UIViewController, BeaconView { ... } ```

    Wednesday, July 9, 2014 3:26 AM
  • User181 posted

    Make sure that you have a constructor like this in RootViewController:

    public RootViewController(IntPtr p) : base(p)
    {
    }
    

    And since your actual class is RootViewController you need to put [Register("RootViewController")] right before the definition of that class.

    When the .xib is loaded it will create the native object and then the mono runtime will find your C# class using the attribute and call the IntPtr constructor to connect the managed world to the native world.

    Wednesday, July 9, 2014 3:59 AM
  • User62728 posted

    Nice, you nailed it. In my cleanup of RootViewController to make it look more like swift, I removed that ctor above. Replacing it just as you suggested resolved the crash.

    Thanks for the help! Much appreciated.

    Wednesday, July 9, 2014 4:03 AM
  • User127880 posted

    I'm seeing this error occurring when my code is scanning bluetooth low-energy peripherals. It works fine for quite a while (discovering nearby devices correctly) but eventually when it discovers one and the event handler of the CBCentralManager.DiscoveredPeripheral is called, when it gets the CBPeripheral object from the CBDiscoveredPeripheralEventArgs (sent with the event) and tries to set an event handler on the DiscoveredService of that CBPeripheral object I get the below Objective-C marshalling error (this same code executes fine for the previously discovered peripherals). Anyone see something l like this before or have an idea what could be causing it?

    {System.Exception: Failed to marshal the Objective-C object 0x17afbf40 (type: CoreBluetoothCBPeripheralCBPeripheralDelegate). 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 (IntPtr ptr, IntPtr klass, System.Type type, MissingCtorResolution resolution) [0x00046] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/ObjCRuntime/Runtime.cs:763 at ObjCRuntime.Runtime.ConstructNSObject[NSObject] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in :0 at ObjCRuntime.Runtime.ConstructNSObject (IntPtr ptr, IntPtr klass, MissingCtorResolution missingCtorResolution) [0x00013] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/ObjCRuntime/Runtime.cs:771 at ObjCRuntime.Runtime.GetNSObject (IntPtr ptr, MissingCtorResolution missingCtorResolution, Boolean evenInFinalizerQueue) [0x00022] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/ObjCRuntime/Runtime.cs:869 at ObjCRuntime.Runtime.GetNSObject (IntPtr ptr) [0x00000] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/ObjCRuntime/Runtime.cs:857 at CoreBluetooth.CBPeripheral.getWeakDelegate () [0x0000b] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/CoreBluetooth/CBPeripheral.g.cs:343 at CoreBluetooth.CBPeripheral.getDelegate () [0x00000] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/CoreBluetooth/CBPeripheral.g.cs:230 at CoreBluetooth.CBPeripheral.EnsureCBPeripheralDelegate () [0x00000] in :0 at CoreBluetooth.CBPeripheral.addDiscoveredService (System.EventHandler`1 value) [0x00000] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/CoreBluetooth/CBPeripheral.g.cs:581 at FleetComplete.Mobile.Core.iOS.Bluetooth.LowEnergy.BluetoothLEDevice..ctor (CoreBluetooth.CBPeripheral nativeDevice, Foundation.NSDictionary advertisementData, Foundation.NSNumber rssi) [0x0004a] in /Users/rnd/Documents/workspace/MFF/FleetComplete.Mobile/FleetComplete.Mobile.Core.iOS/Device/Bluetooth/LowEnergy/BluetoothLEDevice.cs:25 at FleetComplete.Mobile.Core.iOS.Bluetooth.LowEnergy.BluetoothLEProvider.OnDiscoveredPeripheral (System.Object sender, CoreBluetooth.CBDiscoveredPeripheralEventArgs e) [0x00014] in /Users/rnd/Documents/workspace/MFF/FleetComplete.Mobile/FleetComplete.Mobile.Core.iOS/Device/Bluetooth/LowEnergy/BluetoothLEProvider.cs:113 at CoreBluetooth.CBCentralManager+CBCentralManagerDelegate.DiscoveredPeripheral (CoreBluetooth.CBCentralManager central, CoreBluetooth.CBPeripheral peripheral, Foundation.NSDictionary advertisementData, Foundation.NSNumber RSSI) [0x00017] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/build/ios/native/CoreBluetooth/CBCentralManager.g.cs:492 at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:74 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/2077/d230615b/source/maccore/src/UIKit/UIApplication.cs:57 at FleetComplete.UI.Mobile.HOS.iOS.Application.Main (System.String[] args) [0x00008] in /Users/rnd/Documents/workspace/Mobile/FleetComplete.UI.Mobile.HOS.iOS/Main.cs:17 } System.Exception

    Wednesday, September 23, 2015 3:32 PM
  • User181 posted

    That may be a bug in the Xamarin bindings.

    Wednesday, September 23, 2015 4:07 PM
  • User39 posted

    @MarkPevec: can you file a bug (http://bugzilla.xamarin.com) about that?

    Wednesday, September 23, 2015 4:23 PM
  • User127880 posted

    Thanks guys, yes, I'll file a bug at bugzilla.xamarin.com

    Thursday, September 24, 2015 2:57 PM
  • User251444 posted

    hola buena tarde.

    he tenido un problema repetitivo que cierra mi aplicación.

    estoy desarrollando una aplicación en xamarin para Mac tengo un árbol en el cual se encuentran elementos anidados al seleccionar un elemento su contenido se muestra en un CollectionView pero al cambiar varias veces hay un problema con el dispose de algún observer que no se cual es y termina saliéndome el siguiente error.

    Failed to marshal the Objective-C object 0x7fdc57a33de0 (type: __XamarinObjectObserver). Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'Foundation.NSObject+Observer' does not have a constructor that takes one IntPtr argument).

    les agradezco si me pueden ayudar.

    Friday, September 23, 2016 5:20 PM
  • User365328 posted

    I have this in my code in each of my controllers, but still this crash is being reported for a very small amount of users. I'm using mvvm lite with storyboards, any advice? Do you think simply adding the [Register] tag will solve the issue?

    Thursday, April 26, 2018 3:14 PM