Bug 29687 - Xamarin.Mobile 0.7.x - Iterating AddressBook Crashes App
Summary: Xamarin.Mobile 0.7.x - Iterating AddressBook Crashes App
Alias: None
Product: Components
Classification: Xamarin
Component: Xamarin Components ()
Version: Production (addons.xamarin.com)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bill Holmes
Depends on:
Reported: 2015-05-03 18:33 UTC by Mark Fredrickson
Modified: 2015-06-09 16:58 UTC (History)
1 user (show)

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:

Description Mark Fredrickson 2015-05-03 18:33:11 UTC
I just upgraded to Xamarin 5.1, Android Support Library V4, and Xamarin.Mobile 0.7.4 and my app is crashing on many of my customer's phones (including mine). It occurs when iterating through the AddressBook. Even the sample app will crash if used with the Where and OrderBy clauses. If I revert back to Xamarin.Mobile 0.6.3, the app works just fine. The exception that is thrown is:

mscorlib-The given key was not present in the dictionary.
  at System.ThrowHelper.ThrowKeyNotFoundException () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.Dictionary`2[System.String,Xamarin.Contacts.Contact].get_Item (System.String key) [0x00000] in <filename unknown>:0 
  at Xamarin.Contacts.ContactHelper+<GetContacts>c__Iterator2.MoveNext () [0x00000] in <filename unknown>:0 
  at Xamarin.Contacts.ContactHelper+<GetContacts>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0 
  at Xamarin.Contacts.ContactReader+<GetEnumerator>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1E`1[Xamarin.Contacts.Contact].MoveNext () [0x00000] in <filename unknown>:0 
  at BlueFire.Owner..ctor (Android.Content.Context context) [0x00000] in <filename unknown>:0 

The code is below:

AddressBook addressBook = new AddressBook(Application.Context) { PreferContactAggregation = true };

foreach (Contact contact in addressBook.Where(c => c.Emails.Count() > 0))
   // my code   

After hours of debugging I did discover a contact in my phone that had no data associated with it. I don't know how it got there but when I deleted it, the app worked. But Xamarin.Mobile 0.6.3 had no issues iterating through the AddressBook with that bogus contact. I suspect that many other user phones have similar contacts that are causing Xamarin.Mobile 0.7.x to crash.

Unfortunately once I deleted that bogus contact I was unable to re-add it so I cannot duplicate the bug on my phone, only my customers can and it is not possible to debug on their phones (they are not local).

A quick fix would be most appreciated as many of my customers are experience this issue.
Comment 1 Bill Holmes 2015-06-08 17:35:36 UTC
I believe this PR will address the exception.


It is not clear if this resolves an issue or hides one.  The TryGetValue will not throw if the id does not exist.  It is not clear why the id is not being returned.  

I am confident that skipping the id does match the behavior before the map was introduced.  

I am working in merging this into master and updating the component.
Comment 2 Bill Holmes 2015-06-09 16:58:48 UTC
I have merged the pull request and a new version has been added to the store.