Bug in debugger (Thread executes after strange exception) - by Azodious

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 667063 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 5/6/2011 3:16:03 AM
Access Restriction Public


Microsoft Visual Studio 2008
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1

       static ArrayList alist = new ArrayList();

       static void Main(string[] args)
            Program p = new Program();
            Thread removeThread = new Thread(p.StartRemoval);
            Thread addThread = new Thread(p.StartAddition);

         void StartRemoval()
            for (int i = 0; i < 100000; i++)
                alist.Remove(i); // strange exceptio, see stack top: RemoveAt ???
        } // put a debug point - D1
        void StartAddition()
            for (int i = 0; i < 100000; i++)
        } // put a debug point - D2

when exception is received, press F5. thread continues to execute and D1 hits.

System.IndexOutOfRangeException was unhandled
  Message="Index was outside the bounds of the array."
       at System.Collections.ArrayList.RemoveAt(Int32 index)
       at ConsoleApplication3.Program.StartRemoval() in Program.cs:line 85
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
Sign in to post a comment.
Posted by Azodious on 5/23/2011 at 7:05 PM
I agree that it's a race condition.
but as per MSDN: http://msdn.microsoft.com/en-us/library/system.collections.arraylist.remove.aspx, IndexOutOfRangeException should never be thrown.

and, i understand that RemoveAt() is called from within Remove(), but shouldn't Remove() be the stacktop.

Posted by Maria [MSFT] on 5/16/2011 at 8:48 PM
Thank you for submitting this, Azodious.

After investigating the issue, my conclusion is that the debugger is working normally. A first chance exception is caught in Remove() (which calls RemoveAt() which is why you see that at the top of the call stack). That is why the debugger breaks and is able to continue on. You can control breaking from Debug/Exceptions.

The reason for the IndexOutOfRangeException that appears is due to not using a monitor – the two threads are racing for the same resource (the ArrayList), one is trying to take something out and one to put something in. Sometimes the add happens before the remove, and everything works out great. But in some cases it doesn’t.

Thank you,
Visual Studio Debugger
Posted by Microsoft on 5/8/2011 at 11:52 PM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.
Posted by Microsoft on 5/6/2011 at 4:13 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)