IndexOutOfRangeException being thrown from a call to Dictionary.Add?

  • Thread starter Thread starter Adam Clauss
  • Start date Start date
A

Adam Clauss

This does not seem to be a documented exception.
See: http://msdn2.microsoft.com/en-us/library/k7z0zy8k.aspx


System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.Dictionary`2.Resize()
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value,
Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
<our code removed from callstack>

Any ideas on what would cause this?
 
The only thing I can think of is if your key instance is itself throwing
this exception in the GetHashCode() or Equals() methods; do you have any
(cut-down) sample code to illustrate this?

Marc
 
Marc Gravell said:
The only thing I can think of is if your key instance is itself throwing
this exception in the GetHashCode() or Equals() methods; do you have any
(cut-down) sample code to illustrate this?

The Dictionary in question is: Dictionary<string, int> so if
string.GetHashCode were to be throwing this exception, I don't know what I
could do about it anyway...

Unfortunately I'm not sure if I can trim this down any. This is part of a
rather complex system - and we cannot actually duplicate this here in our
development environment. It is being seen at the client's site.

I even poked around using Reflector into that last call to
Dicitonary.Resize, but could not find any reason for
IndexOutOfRangeException to be thrown.
 
OK, probably rule that one out then! (assuming you aren't passing in your
own comparer).

Next try: is there any threading going on? Perhaps one thread is removing
things as another thread is causing resize... I can't see any specific lock
in reflector, so I assume that these methods are *not* thread-safe without
your own sync.

Marc
 
aside: if this is a aspnet project and the dictionary is static, then the
same applies even without your own threads

Apols if this seems obvious; it comes as quite a shock to some people...

Marc
 
Marc Gravell said:
aside: if this is a aspnet project and the dictionary is static, then the
same applies even without your own threads

Apols if this seems obvious; it comes as quite a shock to some people...

Marc

Haha, I welcome anything, obvious or otherwise. And no - not ASP.NET. It's
a Windows Service.

Still the threading is a possibility I am looking into. We are doing some
synchronization, but I am wondering if we have a case which is slipping
through.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top