Bug 15191 - OverflowException in Dictionary.cs
Summary: OverflowException in Dictionary.cs
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-10-03 17:20 UTC by Yvan Rodrigues
Modified: 2013-10-07 06:22 UTC (History)
3 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 Yvan Rodrigues 2013-10-03 17:20:27 UTC
App crashes upon startup due to overflow cause by Dictionary.cs:269.

2013-10-03 17:15:31.922 BuzzRetailiOS[9697:c07] Unhandled managed exception: Number overflow. (System.OverflowException)
  at (wrapper managed-to-native) object:__icall_wrapper_mono_array_new_specific (intptr,int)
  at System.Collections.Generic.Dictionary`2[System.IntPtr,System.String].InitArrays (Int32 size) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:276 
  at System.Collections.Generic.Dictionary`2[System.IntPtr,System.String].Init (Int32 capacity, IEqualityComparer`1 hcp) [0x00040] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:271 
  at System.Collections.Generic.Dictionary`2[System.IntPtr,System.String]..ctor (Int32 capacity, IEqualityComparer`1 comparer) [0x00006] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:252 
  at MonoTouch.ObjCRuntime.Class.RegisterMap (Int32 size, MTClassMap* map) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Class.cs:36 
  at MonoTouch.ObjCRuntime.Class.Initialize (MonoTouch.ObjCRuntime.InitializationOptions& options) [0x00012] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Class.cs:31 
  at MonoTouch.ObjCRuntime.Runtime.Initialize (MonoTouch.ObjCRuntime.InitializationOptions& options) [0x00053] in /Developer/MonoTouch/Source/monotouch/src/ObjCRuntime/Runtime.cs:102 


Solution:
CHANGE
    /* Modify capacity so 'capacity' elements can be added without resizing */
    capacity = (int)(capacity / DEFAULT_LOAD_FACTOR) + 1;

TO
    /* Modify capacity so 'capacity' elements can be added without resizing */
    uint desiredCapacity = (uint)((uint)capacity / DEFAULT_LOAD_FACTOR) + 1;
    capacity = desiredCapacity > int.MaxValue ? int.MaxValue : desiredCapacity;

    capacity = (int)(capacity / DEFAULT_LOAD_FACTOR) + 1;


Xamarin.iOS
Version: 7.0.1.4 (Business Edition)
Hash: 4cfca2f
Branch: 
Build date: 2013-20-09 23:14:32-0400
Runtime:
	Mono 3.2.0 ((no/7c7fcc7)
	GTK 2.24.20
	GTK# (2.12.0.0)
	Package version: 302000000
Comment 1 Sebastien Pouliot 2013-10-03 22:17:12 UTC
`capacity` should never be big enough for the solution to be needed.

The issue is different, can you try the workaround suggested in:
http://forums.xamarin.com/discussion/8560/xamarin-ios-7-0-1-4-number-overflow
Comment 2 Yvan Rodrigues 2013-10-04 09:30:39 UTC
Yes I did encounter the forum article that you suggested prior to posting this. I have tried:
- resetting the simulator
- deleting the app from the simulator
- complete reinstall of Xamarin
- complete removal and reinstall of mono and monotouch

In the stack trace, ObjCRuntime.Class.RegisterMap is invoked with capacity==203352084; then Dictionary.ctor is invoked with capacity==203352084; then things go weird, Init is invoked with capacity==-2147483647, and so on. 
This is contrary to my theory above, in which it is Init that overflows the value; however when I step through the code in the debugger, capacity is a positive integer until noted in the report. The values present in the stack trace seem to reflect the value when the method exits, not enters (possibly a debugger bug).

If I create a new empty or HelloWorld project, this isn't invoked. I can't trace any further up, so I don't know where to start looking for the cause of this large value; however my app will not start due to this behaviour.
Comment 3 Yvan Rodrigues 2013-10-04 09:56:51 UTC
FWIW, I rolled back to MonoTouch 6.3.x (I am also diagnosing another erratic problem) and I am not experiencing this exception.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2013-10-04 18:06:17 UTC
The problem is that Class.RegisterMap should never be called with capacity==203352084. This happens because there is a mismatch between the native libmonotouch library and the corresponding managed parts in monotouch.dll (from two different versions of Xamarin.iOS for instance). 

Does this happen with one app only, or with all the apps on your machine?
Comment 5 Yvan Rodrigues 2013-10-05 09:55:31 UTC
This was occurring with only one app. It went away when I downgraded to monotouch 6.3, and remained stable after I upgraded back to 7.x, so your mismatch prognosis sounds correct.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2013-10-07 06:22:21 UTC
I'm closing this then.