Bug 29196 - .NET GC collected objects calls crash app on iOS device but not in iOS simulator
Summary: .NET GC collected objects calls crash app on iOS device but not in iOS simulator
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 8.8.0
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-04-17 10:52 UTC by Jerome Laban
Modified: 2015-04-20 08:21 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Repro (7.03 KB, application/x-zip-compressed)
2015-04-17 13:45 UTC, Jerome Laban

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jerome Laban 2015-04-17 10:52:40 UTC
When .NET backed UIKit elements are GC'ed by the Mono GC, on a device, the following error appears :

015-03-06 10:40:53.031 PjciOS[1010:258598] System.Exception: Failed to marshal the Objective-C object 0x1ac7d5d0 (type: App1.MyControl). 
Could not find an existing managed instance for this object, nor was it possible to create a new managed instance (because the type 'App1.MyControl' does not have a constructor that takes one IntPtr argument).

Additional information:
	Selector: willRemoveSubview:
	Method: App1.MyControl:WillRemoveSubview (UIKit.UIView)

  at (wrapper managed-to-native) Foundation.NSObject:xamarin_release_managed_ref (intptr,intptr)
  at Foundation.NSObject.ReleaseManagedRef () [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:198 
  at Foundation.NSObject+NSObject_Disposer.Drain (Foundation.NSObject ctx) [0x00062] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:805 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],int

In the simulator, this dose not crash the application, and just displays an warning message.
In this case, the control in question is not available anymore, and it is acceptable to ignore this error, and we do not want to have a IntPtr constructor.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-04-17 12:09:26 UTC
This is not that easy to ignore, and in general indicates that something is wrong in the app.

Can you attach your project so that we can reproduce your particular case ourselves and see if it's something can fix?
Comment 2 Jerome Laban 2015-04-17 13:45:17 UTC
Created attachment 10804 [details]
Comment 3 Jerome Laban 2015-04-17 13:49:09 UTC
The repro I added demonstrates that when running in the simulator, the app will not crash, while showing this message :

2015-04-17 10:46:17.503 App5[19997:11365764] warning: Ignoring invocation of an instance method on a NULL instance.

But on an actual device, it will crash in the drain.

We need to be able to write this kind of sequence for memory management, where it is required to dispose objects for proper all sorts of things, like event handlers, etc...

Isn't the wrapper supposed to remove the code handling C# provided overrides, even when it is disposed via the finalizer ?
Comment 4 Rolf Bjarne Kvinge [MSFT] 2015-04-20 08:21:14 UTC
This has been fixed in Xamarin.iOS 8.10, the sample runs fine on both device and simulator.