I have a simple application that adds about 7 million short strings to a HashSet<string>
. Occasionally I get an exception during a call to Hashset.Add(): System.Collections.Generic.HashSet`1.IncreaseCapacity(): Index was outside the bounds of the array.
It's an intermittent problem and seems related to memory, but this is on a win2k8 R2 server with 16 GB, not much else going on, most of that physical memory is available. Any ideas?
An IndexOutOfRangeException exception is thrown when an invalid index is used to access a member of an array or a collection, or to read or write from a particular location in a buffer. This exception inherits from the Exception class but adds no unique members.
The IndexOutOfRangeException is an exception that will be thrown while accessing an element of a collection with an index that is outside of its range. It occurs when an invalid index is used to access a member of a collection.
The HashSet<T>
is not thread-safe. Especially when adding items in a multi-threaded scenario and the internal capacity has to be increased, things can go out of sync.
The instance methods on HashSet<T>
are not thread-safe. In particular, when you attempt to add an element that would cause the set to exceed the bounds of the existing array in more than one thread at a time, the instance variables used to keep track of the size of the set and the last index in the set can be updated in both threads. In particular, if the last index value is updated by the second thread (with a larger value) before the first thread is finished copying the destination array, it could attempt to access an element of the local array that does not exist because the local array was allocated to only hold half as many elements as that allocated by the second thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With