0

A System.ArgumentOutOfRangeException was raised due to a wrong index used for accessing an item in a DataGrid (happened on a shipped version and no user-interaction was made).

Following a portion of the stacktrace we received:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index
   at System.Windows.Media.VisualCollection.get_Item(Int32 index)
   at System.Windows.Controls.UIElementCollection.get_Item(Int32 index)
   at System.Windows.Controls.UIElementCollection.System.Collections.IList.get_Item(Int32 index)
   at System.Windows.Controls.DataGridCellsPanel.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   at System.Windows.UIElement.Arrange(Rect finalRect)
   at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
   at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)
   at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
   [...]

Further down the stacktrace we could also see that virtualization was active for the DataGrid where the exception was thrown. Most likely the error has something to do with deferred loading/virtualization, however, where it happened is still a mystery.

Is it possible to configure WPF or manually add information to track down where an exception was thrown? (at least which class/control/page/window, possibly even which binding)

Robert
  • 1,235
  • 7
  • 17
  • 2
    "accessing an item in a DataGrid" Without any code that shows how you access the data and what data even exists within that grid, it´s impossibel to guess why you get that exception. Having said this you should provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – MakePeaceGreatAgain Aug 03 '18 at 06:40
  • I'm not asking `WHY` I'm getting this exception, but `WHERE` it was raised. I'm also aware that for exact that case it's going to be quite impossible to track it down, nevertheless, for future occasions I would like to know (at least) which class threw the exception (since there are a lot of `DataGrid`s in our application) – Robert Aug 03 '18 at 06:44
  • 1
    @Robert - It is going to be impossible without a [mcve]. Nevertheless, can you not debug using breakpoints until you find the issue? – Enigmativity Aug 03 '18 at 06:48
  • @Enigmativity Maybe I'm not clear enough, so let's try again: Is it somehow possible to add additional information to the stacktrace in the case of exceptions thrown within/from WPF-controls? I'm not asking to find the exception posted above... – Robert Aug 03 '18 at 06:52
  • It seems that you trying to get item from collection that was cleared in wrong way – sTrenat Aug 03 '18 at 06:53
  • @sTrenat I know that it has something to do with that, however to find the location where such an exception was thrown, I'm asking if it's possible to add further information to the stacktrack (or log it somehow) – Robert Aug 03 '18 at 06:55
  • @Robert - Perhaps you should change your question from the super clear **"Where was an exception thrown?"** to something that is clearer and easier to read than **"Is it possible to configure WPF or add additional information (manually) to track down where (at least which class/control/page/window, possibly even which binding) caused the error?"**? – Enigmativity Aug 03 '18 at 06:56
  • 1
    @Robert you might want to reformulate your question. You last paragraph is probably wording it a bit better. – Mixxiphoid Aug 03 '18 at 06:57
  • 1
    since it was thrown from PresentationFramework you probably cannot. But I guess that you trying to reuse some ViewModel that was bind to some View and had some collection – sTrenat Aug 03 '18 at 06:57
  • 1
    Does ticking the "Common Language Runtime Exceptions" checkbox in the Exception Settings window help? – Élie Aug 03 '18 at 06:57
  • @Gui Happened in production mode (never happened when running with a debugger) , so no, enabling the checkbox does not help – Robert Aug 03 '18 at 07:15
  • Is this what you are looking for? [Show line number in exception handling](https://stackoverflow.com/q/688336/3744182) and [C# - get line number which threw exception](https://stackoverflow.com/q/3328990/3744182). – dbc Aug 03 '18 at 07:28
  • @dbc unfortunately no... we already get/log the exception with the full stacktrace, however, it's not really meaningful (does not show file/class/line-number) – Robert Aug 03 '18 at 07:38
  • Is that the fulll stack trace? – Mel Gerats Aug 03 '18 at 07:56
  • @MelGerats part of it... but the other half is as meaningful as the posted one. – Robert Aug 03 '18 at 08:05
  • Did you find the cause? We have the exact (by stack) same problem. Thank you. – zdf Sep 24 '20 at 09:15

2 Answers2

1

I suspect your best bet is to get your hands on a memory dump to diagnose the issue. How to obtain one, depends on how your exception handling code is set up.

If your application handles the exception itself (and does not crash), it will be hard to generate a dump. You could try the approach outlined here: Generating .NET crash dumps automatically. Alternatively, if you keep the application alive (with a dialog box or something) when the exception is thrown, you could ask the user to get a memory dump for further analysis (via task manager => right click on process => Create dump).

If your application actually crashes, a dump file can be created automatically, but this needs to be configured in the registry (see https://blogs.msdn.microsoft.com/tess/2010/08/23/getting-full-user-mode-dumps-automatically-when-your-process-crashes/, https://www.meziantou.net/2018/06/04/tip-automatically-create-a-crash-dump-file-on-error).

jeroenh
  • 26,362
  • 10
  • 73
  • 104
0

I did something like below in App.xaml.cs file

  /// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    static string filePath = System.IO.Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData), "Log.txt");
    private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        using (StreamWriter writer = new StreamWriter(filePath, true))
        {
          writer.WriteLine("Message :" + e.Exception.Message + "<br/>" + Environment.NewLine + "StackTrace :" + e.Exception.StackTrace +  "" + Environment.NewLine + "Date :" + Now.ToString());
          writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
        }
        MessageBox.Show("An unhandled exception just occurred: " + e.Exception.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
        e.Handled = true;
    }
}

Hope it will help you

RackM
  • 449
  • 7
  • 26
  • Thanks for the effort, however, we do already log those exceptions. The problem is that the stacktrace only tells us that the exception occurred somewhere in the PresentationFramework, but not exactly which class/control. – Robert Aug 03 '18 at 07:21