dcrobbins
dcrobbins

Reputation: 525

IndexOutOfRangeException when adding to Hashset<T>

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?

Upvotes: 29

Views: 7636

Answers (2)

tvanfosson
tvanfosson

Reputation: 532455

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.

Upvotes: 8

herzmeister
herzmeister

Reputation: 11287

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.

Upvotes: 54

Related Questions