n00b
n00b

Reputation: 6330

Locating the source of an UnhandledException

My program seems to have a bug in it which I'm finding hard to track down. The program is multithreaded and quite complex. In a nutshell it communicates with multiple clients using asynchronous sockets and writes to the database.

A System.Reflection.TargetInvocationException is thrown when I hammer the program with client communications and hold down the enter key to spam writes into the database. At some point in time the exception is thrown which I'm catching with the following snippet of code in the constructor of the main window:

    VMMainWindow()
    {
         ...
         System.Windows.Threading.Dispatcher.CurrentDispatcher.UnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(CurrentDispatcher_UnhandledException);
         ....
    }

    static void CurrentDispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        string str = e.ToString();
    }

I don't have much to work with but I was wondering if someone could suggest some tools or or point me in the right direction to help me detect the location of this exception. The exception in detail is pasted below.

System.Reflection.TargetInvocationException was unhandled   Message=Exception has been thrown by the target of an invocation.   Source=mscorlib   StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at SupernovaServer.App.Main() in D:\Projects\Supernova Server\Server\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()   InnerException: System.ArgumentOutOfRangeException
       Message=Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
       Source=mscorlib
       ParamName=index
       StackTrace:
            at System.ThrowHelper.ThrowArgumentOutOfRangeException()
            at System.Collections.Generic.List`1.get_Item(Int32 index)
            at System.Collections.ObjectModel.Collection`1.System.Collections.IList.get_Item(Int32 index)
            at System.Windows.Data.ListCollectionView.AdjustBefore(NotifyCollectionChangedAction action, Object item, Int32 index)
            at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
            at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
       InnerException:

Upvotes: 2

Views: 8948

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 133950

Is it possible that you have multiple threads making concurrent accesses to a collection that isn't thread-safe? Say, one thread removing an item while another is reading or adding? That would definitely cause this type of error. In general, the .NET collection classes (except for those in System.Collections.Concurrent) are not thread-safe. You can have multiple readers, but any modification of the collection is going to require exclusive access--protected by a lock or a reader/writer lock.

Upvotes: 4

Zenexer
Zenexer

Reputation: 19613

It's pretty safe to assume App.g.cs is not where the problem lies, so as you said, there isn't much to go on. So here's my hint to you: look at the inner exception. Those things are quite handy, and I'll be willing to bet you have an inner exception at least one layer deep.

Upvotes: 1

Related Questions