views:

155

answers:

1

I'm getting an Out of Index Error when adding an Object to a System.Collections.Generic.List

Dim myObj As New MyObject
Dim List As New List(Of MyObject)
List.Add(myObj)

The error message are these (translated from my system language):
"Index out of Matrix indexes"
"Matrix origin wasn't long enough"
"Verify srcIndex, size and inferior limits of the matrix"

Shouldn't .NET automatically resize the size of the List?

I cannot replicate this error. It happenes once in hundrededs of operations.
This is running in threads, but each thread has it's own List.
I've tried a SyncLock on the List but without any effect.

Anyone knows what can be the problem and how to avoid it?

A: 

This sounds like a concurrency issue, .Net does automatically resize the underlying array when it reaches capacity.

.Net collections are not thread safe (that is coming in the next version of .NET)

Are you locking gets as well as sets? If so it should work. You can also look at readerwriter locks if you want to be fancy.

Sam Saffron
Should add that `List<T>` still isn't thread safe in the net version of .NET, but there are all-new collection classes that are. :)
280Z28