Bug 15889 - System.Collections.Generic.Dictionary.ContainsKey(), Array index is out of range.
Summary: System.Collections.Generic.Dictionary.ContainsKey(), Array index is out of ra...
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.8.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-11-02 06:39 UTC by Justin Caldicott
Modified: 2017-08-07 20:38 UTC (History)
2 users (show)

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

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:
Status:
RESOLVED INVALID

Description Justin Caldicott 2013-11-02 06:39:51 UTC
Hi,

We updated to 4.8.x recently, and have done our first release to Beta testers since this.

One of these testers (running a Samsung GT-I9300 (Android 4.1.2 Phone)) is consistently seeing exceptions, which AFAIK shouldn't happen.  Nothing changed in our code that is likely to have affected it.

The MS documentation on Dictionary.ContainsKey() indicates that the only exception you should expect from this is ArgumentNullException: http://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx

It hasn't happened for any of our other (9) testers, even though one of them has the same device and OS version.

Any help would be greatly appreciated.  I don't want to deploy this build beyond our testers until I've fixed this.

Best wishes,

Justin
Comment 1 Jonathan Pryor 2013-11-02 16:43:16 UTC
Can you provide the complete stack trace?

Is the Dictionary being accessed in a multi-threaded fashion?
Comment 2 Justin Caldicott 2013-11-02 17:45:26 UTC
No, there's only the one thread using the Dictionary AFAIK.  Multiple dictionary instances have shown the problem for the same user.  Both stack traces are below.  They're both during application startup.

Let me know if you need any more detail.  This started after upgrading to 4.8.x, though I don't recall what version we were using before that.

Many thanks for the prompt response.

Best wishes,

Justin

Message: Array index is out of range.

Stack trace:
at System.Collections.Generic.Dictionary`2<BirdJournal.Data.Taxon, bool>.ContainsKey (BirdJournal.Data.Taxon) <0x00094> 
at LfFramework.Collections.UniqueList`1<BirdJournal.Data.Taxon>.Add (BirdJournal.Data.Taxon) <0x0003b> 
at BirdJournal.Data.TaxonomicSequence..ctor (System.Collections.Generic.IEnumerable`1<BirdJournal.Data.Taxonomy>) <0x004df> 
at BirdJournal.Data.TaxonomicConfig..ctor (BirdJournal.Data.Taxonomy,BirdJournal.Data.Taxonomy,System.Collections.Generic.IEnumerable`1<BirdJournal.Data.Taxonomy>) <0x003a7> 
at BirdJournal.Mobile.Application.InitialiseDatabase () <0x000ef> 
at BirdJournal.Droid.AndroidApplication.OnCreate () <0x0023b> 
at Android.App.Application.n_OnCreate (intptr,intptr) <0x00037> 
at (wrapper dynamic-method) object.b1c03069-7310-40ce-8482-ec950fd0a7d7 (intptr,intptr) <0x0003b>

Message: Array index is out of range.

Stack trace:
at System.Collections.Generic.Dictionary`2<BirdJournal.Data.Concept, bool>.ContainsKey (BirdJournal.Data.Concept) <0x00094> 
at LfFramework.Collections.UniqueList`1<BirdJournal.Data.Concept>.Add (BirdJournal.Data.Concept) <0x0003b> 
at LfFramework.Collections.UniqueList`1<BirdJournal.Data.Concept>.AddRange (System.Collections.Generic.IEnumerable`1<BirdJournal.Data.Concept>) <0x0009b> 
at BirdJournal.Data.Location.get_Concepts () <0x000af> 
at BirdJournal.Data.Location.get_Concepts () <0x0009f> 
at BirdJournal.Data.Location.get_Concepts () <0x0009f> 
at BirdJournal.Data.Location.get_Concepts () <0x0009f> 
at BirdJournal.Data.Database.updateAllConceptsIndex () <0x00037> 
at BirdJournal.Data.Database.updateConcepts () <0x00dbb> 
at BirdJournal.Data.Database.Init (string,bool,BirdJournal.Data.Platform,BirdJournal.Data.DatabasePathInfo,BirdJournal.Data.Database/TaxonMigrationEventHandler,bool) <0x00533> 
at BirdJournal.Mobile.Application.InitialiseDatabase () <0x00063> 
at BirdJournal.Droid.AndroidApplication.OnCreate () <0x0023b> 
at Android.App.Application.n_OnCreate (intptr,intptr) <0x00037> 
at (wrapper dynamic-method) object.6eea678b-b2aa-4c46-b7a9-f86bcdfaaf8e (intptr,intptr) <0x0003b>
Comment 3 Jonathan Pryor 2013-11-04 13:54:08 UTC
Alas, I can't imagine why this would be happening. :-(

Would it be possible for you to create a small testcase that reproduces the exception?

Thanks,
 - Jon
Comment 4 Justin Caldicott 2013-11-04 16:10:50 UTC
Hi Jon,

I'm not able to reproduce it locally, it's only happening on this one user's device.

Not sure how to progress this.. is there more logging that I can enable for him somehow?

Thanks,

Justin
Comment 5 Jonathan Pryor 2013-11-04 16:48:20 UTC
@Justin: Which device is this?

As for what else to do, you could do this:

http://docs.xamarin.com/guides/android/advanced_topics/environment/

Add a plain text-file to your .csproj, set the Build action to AndroidEnvironment, and add:

debug.mono.debug=1

IFF this is a Debug build (i.e. debug symbols are on the target device...), then the exception will contain line numbers, which will narrow down where within ContainsKey() things are going bad:

https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Collections.Generic/Dictionary.cs#L474

EIther it's Dictionary.table (line 481) or it's Dictionary.linkSlots or keySlots (line 487) which is generating the exception; it would be handy to know which.

Though it doesn't look possible for line 481 to throw (due to the % operator use), suggesting it's the linkSlots/keySlots access which is failing, which in turn suggests that the "linked list" behavior of the hashtable is at fault...

...which means it would be _really_ handy to have an actual test case. :-(
Comment 6 Justin Caldicott 2013-11-04 17:40:59 UTC
Jonathan,

It's a UK Samsung GT-I9300 (Android 4.1.2) phone.

I'm just waiting for those changes to run through our build system and I'll get them to the tester and hopefully get more detail.

I'll post back when I have more data.

Thanks,

Justin
Comment 7 Jon Douglas [MSFT] 2017-08-07 20:38:59 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Android. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.