Bug 24609 - Random crashes (possibly due to switched object pointers)
Summary: Random crashes (possibly due to switched object pointers)
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: XI 8.4.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 9.2 (C6)
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2014-11-18 08:47 UTC by tom.quist
Modified: 2015-11-16 16:09 UTC (History)
5 users (show)

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

example project to reproduce crashes (4.05 MB, application/zip)
2014-11-18 08:47 UTC, tom.quist

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 tom.quist 2014-11-18 08:47:56 UTC
Created attachment 8780 [details]
example project to reproduce crashes

In our project we get random crashes within unmanaged code. Here some of the exceptions we get:

When calling NSLayoutConstraint.Create():
Unhandled managed exception: Unable to cast object of type 'PointerSwitchBug.LinearLayoutView' to type 'MonoTouch.UIKit.NSLayoutConstraint' (System.InvalidCastException)
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject[NSLayoutConstraint] (IntPtr ptr) [0x000e6] in /Developer/MonoTouch/Source/maccore/src/ObjCRuntime/Runtime.iOS.cs:525 
  at MonoTouch.UIKit.NSLayoutConstraint.Create (MonoTouch.Foundation.NSObject view1, NSLayoutAttribute attribute1, NSLayoutRelation relation, MonoTouch.Foundation.NSObject view2, NSLayoutAttribute attribute2, Single multiplier, Single constant) [0x00016] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/NSLayoutConstraint.g.cs:134 

When calling DequeueReusableCell() on UITableView:
Unhandled managed exception: Unable to cast object of type 'PointerSwitchBug.LinearLayoutView' to type 'MonoTouch.UIKit.UITableViewCell' (System.InvalidCastException)
  at MonoTouch.ObjCRuntime.Runtime.GetNSObject[UITableViewCell] (IntPtr ptr) [0x000e6] in /Developer/MonoTouch/Source/maccore/src/ObjCRuntime/Runtime.iOS.cs:525 
  at MonoTouch.UIKit.UITableView.DequeueReusableCell (MonoTouch.Foundation.NSString identifier) [0x0001c] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UITableView.g.cs:384 

When accessing the property 'Constraints' on our UIView subclass:
Unhandled managed exception: Unable to cast object of type 'PointerSwitchBug.LinearLayoutView' to type 'MonoTouch.UIKit.NSLayoutConstraint'. (System.InvalidCastException)
  at MonoTouch.ObjCRuntime.Runtime.GetINativeObject[NSLayoutConstraint] (IntPtr ptr, Boolean owns) [0x00075] in /Developer/MonoTouch/Source/maccore/src/ObjCRuntime/Runtime.iOS.cs:743 
  at MonoTouch.Foundation.NSArray.UnsafeGetItem[NSLayoutConstraint] (IntPtr handle, UInt32 index) [0x00008] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSArray.cs:303 
  at MonoTouch.Foundation.NSArray.ArrayFromHandle[NSLayoutConstraint] (IntPtr handle) [0x00028] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSArray.cs:244 
  at MonoTouch.UIKit.UIView.get_Constraints () [0x00030] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/UIView.g.cs:2859 

And on many more places. It looks like our custom View messes up the runtime and somehow pointers to Objective-C objects get switched randomly. I created a minimal example project which illustrates the problem. You can reproduce the crashes by running the project in simulator and rotate the device a couple of times. I think it has to do with how GetHeightForRow() in MasterViewController is implemented. If I remove the method from the UITableViewSource, everything seems to work.

This is a very critical bug for us atm. and we didn't find a workaround by now.

Xamarin Studio
Version 5.5.3 (build 6)
Installation UUID: 46f5c403-77ef-4867-a81a-640324614b30
	Mono 3.10.0 ((detached/e204655)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000023

Apple Developer Tools
Xcode 6.1 (6604)
Build 6A1052d

Version: (Business Edition)
Hash: 80e9ff7
Build date: 2014-10-22 15:09:12-0400

Build Information
Release ID: 505030006
Git revision: fbe3e9453daf6a3bb9a9709ed22bec35f7c9056b
Build date: 2014-10-23 13:08:38-04
Xamarin addins: e44add2b39de4dd57c0742bb2e620dfad84c64c6

Operating System
Mac OS X 10.9.4
Darwin XIN842076.asv.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 1 tom.quist 2014-11-18 09:03:43 UTC
Ok I nailed it down to a simple method. Remove everything except the constructor from the class LinearLayoutView (also the properties and remove the corresponding property attributes in the storyboard) and just implement WillRemoveSubview() as follows:

public override void WillRemoveSubview(UIView uiview)

When implementing the method, the problem occurs. When removing the method, the problem is gone. Very strange...
Comment 2 Rolf Bjarne Kvinge [MSFT] 2014-11-25 03:41:45 UTC
I can reproduce.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2014-11-26 07:28:36 UTC

maccore/master: 02474ac1dd4182feebf4adc103c0bce4a07809fc
monotouch/master: 1c67fefbbb7693c71fd390f2067a06584f91885b

The fix will be included in the next main release after 8.6 (excluding any extra releases we make due to WatchKit support).
Comment 4 Sebastien Pouliot 2015-06-04 20:13:49 UTC
Note that this fix caused regressions (tracked in #29801) and is being removed in XI 8.10.2. A different fix is being tested and is scheduled to be released with XI 8.12.

If this bug was affecting anyone (beside Tom) then you better remain with XI 8.10.1 (or a previous version) until it's resolved completely.
Comment 5 GouriKumari 2015-11-16 16:09:01 UTC
I followed the steps mentioned to comment# 0, but couldn't reproduce the issues with cycle6 builds. App build and deployed on simulator successfully and app didn't crash on rotating simulator left and right.

Application Output: https://gist.github.com/GouriKumari/611803bdc8f5c33c8986
Test Env:
=== Xamarin Studio ===

Version 5.10 (build 871)
Installation UUID: 5ed3a124-4b77-4c6f-beb9-c830fd815e2a
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.1 (9079)
Build 7B91b

=== Xamarin.iOS ===

Version: (Enterprise Edition)
Hash: 3c0ec35
Branch: master
Build date: 2015-11-12 13:05:39-0500

=== Xamarin.Android ===

Version: (Enterprise Edition)
Android SDK: /Users/gourikumari/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)

SDK Tools Version: 22.6.3
SDK Platform Tools Version: 19.0.2
SDK Build Tools Version: 19.1

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Version: (Enterprise Edition)

=== Build Information ===

Release ID: 510000871
Git revision: 4e9c5abb5ffdae12ba02ac49da83f8b2011dbb88
Build date: 2015-11-12 06:02:54-05
Xamarin addins: 55007ed0e56436f385d8e26394a45be563abc7e8
Build lane: monodevelop-lion-cycle6

=== Operating System ===

Mac OS X 10.10.5