tags:

views:

31

answers:

1

I have a application that clients connect to via socket. The application is multi threaded, does lots of number crunching, and consumes a huge ammount of memory. (1.5 - 2 GB) Occasionally it throws this error in a seemingly simple area of code.

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Its not always on the same line, but its always in the same file, on a foreach loop. I'm wondering if theres some setting in VS 2008 thats causing it? Theres nothing weird going on it really just dies on a foreach loop iterating through a List. I'm pretty sure its not a threading issue because I am using lock() so i'm 100% sure that the Lists arnt getting modified by multiple threads at the same time. I've tried turning off compiler optimizations but it still does it every once in a while. Very annoying.

This is a very very rare occurance. I'm running the server in debug and this only happens like once every 12-48 hours.

+1  A: 

"Theres nothing weird going on it really just dies on a foreach loop iterating through a List". - That sounds suspiciously like a threading synchronisation issue. (In fact, I can 99% guarantee it is!)

You probably need to take a copy of the list, if you have other threads attempting to modify it while you are iterating over it.

Suggest you post some code.

Mitch Wheat
The block of code is wrapped with a lock() so i'm sure the lists arnt being modified. The objects in the list might be though, by threads that already have a reference. Also it never throws a collection modified error, so if it was another thread modifing the list i would think that would happen also.
Nathan