Bug 17547 - Dispatcher.BeginInvoke throws ArgumentException("length")
Summary: Dispatcher.BeginInvoke throws ArgumentException("length")
Alias: None
Product: Class Libraries
Classification: Mono
Component: WindowsBase ()
Version: master
Hardware: PC All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-02-01 18:20 UTC by Dmitry Potapov
Modified: 2014-02-03 04:01 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Patch to DispatcherOperation (1.17 KB, patch)
2014-02-01 18:20 UTC, Dmitry Potapov

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Dmitry Potapov 2014-02-01 18:20:19 UTC
Created attachment 5958 [details]
Patch to DispatcherOperation

Executing a code like this:

  dispatcher.BeginInvoke(DispatcherPriority.Normal, someDelegate, arg1, arg2);

causes the following exception:

System.ArgumentException: length
  at System.Array.Copy (System.Array sourceArray, Int32 sourceIndex, System.Array destinationArray, Int32 destinationIndex, Int32 length) [0x0020f] 
  at System.Windows.Threading.DispatcherOperation..ctor (System.Windows.Threading.Dispatcher dis, DispatcherPriority prio, System.Delegate d, System.Object arg, System.Object[] args) [0x0002a]
  at System.Windows.Threading.Dispatcher.BeginInvoke (DispatcherPriority priority, System.Delegate method, System.Object arg, System.Object[] args) [0x00041] 

After studying the source code, the bug appears in the DispatcherOperation constructor:

internal DispatcherOperation (Dispatcher dis, DispatcherPriority prio, Delegate d, object arg, object [] args) 
        : this (dis, prio)
        delegate_method = d;
        delegate_args = new object [args.Length + 1];
        delegate_args [0] = arg;
        Array.Copy (args, 1, delegate_args, 0, args.Length);

When the array is copied, the source and the destination indexes are confused.

I have attached the patch.