Bug 32931

Summary: NullReferenceException in KeyValuePair.get_Key when using generic method and Linq
Product: iOS Reporter: mfulker
Component: Mono runtime / AOT compilerAssignee: Zoltan Varga <vargaz>
Severity: normal CC: kumpera, mono-bugs+monotouch, ramc, rolf, sebastien
Priority: Normal    
Version: master   
Target Milestone: 8.13 (C6 alpha)   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: project that reproduces the exception

Description mfulker 2015-08-10 17:25:12 UTC
Created attachment 12436 [details]
project that reproduces the exception

The relevant code:

public override void ViewDidLoad()

   // Perform any additional setup after loading the view, typically from a nib.

      Dictionary<long, IList<double?>> data = new Dictionary<long, IList<double?>>
         {123L, new List<double?> {2.0, null, 5.5}}
      Dictionary<long, double?> newDict = ConvertDictionary(data);
   catch (Exception e)

private static Dictionary<long, TValue> ConvertDictionary<TValue>(Dictionary<long, IList<TValue>> source)
   return source.ToDictionary(pair => pair.Key, pair => pair.Value[0]);

This causes this exception on an actual iPad. (It works fine on a simulator)

System.NullReferenceException: Object reference not set to an instance of an object

2015-08-10 15:53:52.621 GenericsIssueApp1[6182:4113422] Unhandled managed exception:
Object reference not set to an instance of an object (System.NullReferenceException)
  at System.Collections.Generic.KeyValuePair`2[System.Int64,System.Collections.Generic.IList`1[System.Nullable`1[System.Double]]].get_Key () [0x00000] in /Users/builder/data/lanes/1962/8b265d64/source/mono/external/referencesource/mscorlib/system/collections/generic/keyvaluepair.cs:36 
  at (wrapper unknown) object:gsharedvt_out ()
  at GenericsIssueApp1.RootViewController.<ConvertDictionary>b__2[Nullable`1] (KeyValuePair`2 pair) [0x00000] in c:\development\GenericsIssueApp1\GenericsIssueApp1\RootViewController.cs:57 
  at (wrapper delegate-invoke) System.Func`2<System.Collections.Generic.KeyValuePair`2<long, System.Collections.Generic.IList`1<System.Nullable`1<double>>>, long>:invoke_TResult_T (System.Collections.Generic.KeyValuePair`2<long, System.Collections.Generic.IList`1<System.Nullable`1<double>>>)
  at System.Linq.Enumerable.ToDiction
ary[KeyValuePair`2,Int64,Nullable`1] (IEnumerable`1 source, System.Func`2 keySelector, System.Func`2 elementSelector, IEqualityComparer`1 comparer) [0x0002f] in //Library/Frameworks/Xamarin.iOS.framework/Versions/ 
  at System.Linq.Enumerable.ToDictionary[KeyValuePair`2,Int64,Nullable`1] (IEnumerable`1 source, System.Func`2 keySelector, System.Func`2 elementSelector) [0x00000] in //Library/Frameworks/Xamarin.iOS.framework/Versions/ 
  at GenericsIssueApp1.RootViewController.ConvertDictionary[Nullable`1] (System.Collections.Generic.Dictionary`2 source) [0x00001] in c:\development\GenericsIssueApp1\GenericsIssueApp1\RootViewController.cs:57 
  at GenericsIssueApp1.RootViewController.ViewDidLoad () [0x0005d] in c:\development\GenericsIssueApp1\GenericsIssueApp1\RootViewController.cs:44
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-08-11 06:57:47 UTC
@Zoltan, I can reproduce this on device with both armv7 and arm64, but it does not happen in the simulator.
Comment 2 Ram Chandra 2015-08-11 06:59:00 UTC
I have checked this issue and I am able to reproduce this issue. I have checked the same on simulator but I am not getting the Null reference exception on simulator. The NRE exist on iPad.

Screencast: http://www.screencast.com/t/ioBJLwBrZCj

Application output: https://gist.github.com/Udham1/b0f19945c718eb6f98ad
IDE logs: https://gist.github.com/Udham1/22f59f8924c4569038bb
Device logs: https://gist.github.com/Udham1/d4b01865a1646a61c494

Environment Info:

iPad: 8.4
=== Xamarin Studio ===

Version 5.9.5 (build 9)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
	Mono 4.0.3 ((detached/d6946b4)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400030020

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 7e598ef
Branch: master
Build date: 2015-08-08 12:28:27-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/xamarin76/Desktop/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Build Information ===

Release ID: 509050009
Git revision: b99cb19a1d3b15d8980c4616f13f837457e5b292
Build date: 2015-07-28 02:01:44-04
Xamarin addins: d53d6eceb828422f3d4aff8547f2f6bab6c9ea58

=== Operating System ===

Mac OS X 10.9.5
Darwin Xamarin76s-Mac-mini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 3 Sebastien Pouliot 2015-08-13 12:02:32 UTC
Confirmed it's not linker related (works in simulator/linking, fails on devices/not linking)
Comment 4 Zoltan Varga 2015-08-14 20:38:39 UTC
Should be fixed by mono mono-4.2.0-pre2-branch 917c8f6b125f649aec21aae643c78563cb6b27a1/ and mono-extensions 137f7b9b070fe365875b0b593ea87fb6ef16e1a2. The fix will be in an later xamarin.ios release.