views:

212

answers:

2

When I add an ApplicationCommands command to a MenuItem in my file menu, no matter via XAML or via code, when I open the menu the application crashes in a stack overflow, with absolutely no details about the problem. When I remove the Command, the problem also disappears. It doesn't matter which ApplicationCommand I use.

Part of the call stack:

  • WindowsBase.dll!MS.Utility.ArrayItemList.ArrayItemList(int size) + 0x20 bytes
  • WindowsBase.dll!MS.Utility.FrugalStructList.Capacity.set(int value) + 0x6a bytes
  • WindowsBase.dll!MS.Utility.FrugalStructList.FrugalStructList(int size) + 0x9 bytes
  • PresentationCore.dll!System.Windows.EventRoute.EventRoute(System.Windows.RoutedEvent routedEvent) + 0x35 bytes
  • PresentationCore.dll!System.Windows.EventRouteFactory.FetchObject(System.Windows.RoutedEvent routedEvent) + 0x31 bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x3f bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool trusted) + 0x35 bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(object parameter, System.Windows.IInputElement target, bool trusted, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 bytes
    PresentationCore.dll!System.Windows.Input.RoutedCommand.CanExecuteImpl(object parameter = null, System.Windows.IInputElement target = {System.Windows.Controls.RichTextBox}, bool trusted = false, out bool continueRouting = false) + 0x70 bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecute(object parameter, System.Windows.IInputElement target, bool trusted, out bool continueRouting) + 0x3a bytes
  • PresentationCore.dll!System.Windows.Input.CommandManager.TransferEvent(System.Windows.IInputElement newSource, System.Windows.Input.CanExecuteRoutedEventArgs e = {System.Windows.Input.CanExecuteRoutedEventArgs}) + 0x52 bytes
  • PresentationCore.dll!System.Windows.Input.CommandManager.OnCanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) + 0x8c bytes
    PresentationCore.dll!System.Windows.UIElement.OnCanExecuteThunk(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e) + 0x44 bytes
  • PresentationCore.dll!System.Windows.Input.CanExecuteRoutedEventArgs.InvokeEventHandler(System.Delegate genericHandler, object target) + 0x41 bytes
    PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x27 bytes PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) + 0x3e bytes
    PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool reRaised = false) + 0x1bf bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.RichTextBox}, System.Windows.RoutedEventArgs args = + 0x79 bytes
  • PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.CanExecuteRoutedEventArgs}, bool trusted) + 0x35 bytes
  • PresentationCore.dll!System.Windows.Input.RoutedCommand.CriticalCanExecuteWrapper(object parameter, System.Windows.IInputElement target, bool trusted, System.Windows.Input.CanExecuteRoutedEventArgs args) + 0x80 bytes

It looks like the application is stuck in an endless loop. Is this my fault (and what am I doing wrong) or a bug in .NET 3.5?

I use this code:

MenuItem mi = new MenuItem();
mi.Command = ApplicationCommands.Open;
FileMenu.Items.Add(mi);

It doesn't matter wheter I create the menuItem via code or in XAML, and like I said setting the Command also doesn't matter where. The problem also occurs when using MediaCommands, so I guess for all Commands in general.

The RichTextBox code:

//configure richtextbox
sb = new RichTextBox();
sb.Margin = new Thickness(-3);
sb.BorderThickness = new Thickness(0);
sb.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
sb.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
///TODO: get font from preferences.
FontFamilyConverter ffc = new FontFamilyConverter();
sb.FontFamily = (FontFamily)ffc.ConvertFromString("Lucida Sans Unicode");
sb.FontSize = 13;
sb.AcceptsReturn = true; sb.AcceptsTab = true;
sb.AllowDrop = true; sb.IsDocumentEnabled = false;
sb.Padding = new Thickness(5);

//markup styles
Style s = new Style(typeof(Paragraph));
s.Setters.Add(new Setter(Paragraph.MarginProperty, new Thickness(0)));
sb.Resources.Add(typeof(Paragraph), s);

this.AddChild(sb);

The RichTextBox is added in the constructor of a control that is derived from TabItem.

A: 

Yes, this is your fault and not a bug in 3.5 (hey, you asked). Now to find your bug...

From the stack trace, there are a lot of CanExecute events, and they seem to be associated with a RichTextBox. Do you have any CanExecute logic in your code? Honestly, we need more code in order to help effectively.

Also, an endless loop and stack overflow are different. Does your program ever blow up with the SO exception or does it just keep running forever?

Erich Mirabal
No, just the StackOverflow error. I don't have any CanExecute logic in my code. (I added the RichTextbox code to my question).
Ruud v A
You must have to some sort of recursive/circular call in there. Do you have any event handlers doing some logic they shouldn't be?
Erich Mirabal
I did some other test, and it turns out that not all commands return error... ApplicationCommands.Undo doens't.
Ruud v A
A: 

I found the problem. I was adding my RichTextBox to a focus group. I removed that and now it works. Altough I still can't get the keyboard focus in the RichTextBox at startup. (Tried Keyboard.Focus(sb), sb.Focus(), sb.Document.Focus(), Keyboard.Focus(sb.Document), FocusManager.SetFocusedElement(this, sb), ect...

Ruud v A