4
votes

I have spent a good day trying to figure out why the PasswordBox is causing me so many problems.
I have an application that works on every machine I have tested it on. Now it has been installed on other machines it is crashing whenever I click inside the PasswordBox.
I even made a test application with just a TextBox and PasswordBox control to see if the crash was caused by any underlying code in my application, but this turned out not to be the case. I click on the TextBox, and it works fine. Allows me to type text inside it. But as soon as I click on the PasswordBox, application crashes.

I set up these events to catch any unhandled exceptions:

  • Application.Current.DispatcherUnhandledException
  • AppDomain.CurrentDomain.UnhandledException
  • AppDomain.CurrentDomain.FirstChanceException

Now this should show me a MessageBox if any errors occur. The worst part is, is that there aren't any errors thrown to these events.
I set up some logging in my application and I got exceptions like:

  • Object reference is not set to an instance of an object
  • Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I had set up an event for the PasswordBox to test if it got focus before it crashed and it did, so I don't think that the Object reference exception refers to the PasswordBox control.
I have been searching everywhere to find if anyone else has had these issues and have only seen issues with the TextBox control and its Focus issues, which are supposed to be fixed in .NET 4.0

Oh! and that reminds me. I am using WPF .NET 4.0.

Any help would be greatly appreciated.

The test code is:

private void Application_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler((x, y) => { ShowError(x, y); });  

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((x, y) => { ShowError(x, y); });
    AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>((x, y) => { ShowError(x, y); });
}

private void ShowError(object sender, object o)
{
    Exception ex = null;
    if (o.GetType() == typeof(FirstChanceExceptionEventArgs))
    {
        ex = ((FirstChanceExceptionEventArgs)o).Exception;
    }
    else if (o.GetType() == typeof(DispatcherUnhandledExceptionEventArgs))
    {
        ex = ((DispatcherUnhandledExceptionEventArgs)o).Exception;
    }
    else if (o.GetType() == typeof(UnhandledExceptionEventArgs))
    {
        ex = (Exception)((UnhandledExceptionEventArgs)o).ExceptionObject;
    }
    MessageBox.Show(ex.Message + "\n\r" + ex.StackTrace, "Error at: " + ex.Source, MessageBoxButton.OK, MessageBoxImage.Error);
}

And the Xaml:

<Window x:Class="WPF_Control_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="200" Width="300">
    <Grid>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left">
            <StackPanel Orientation="Horizontal" Width="260">
                <TextBlock Text="TextBox: " TextAlignment="Right" Width="80" Height="Auto" VerticalAlignment="Center" />
                <TextBox Name="textBox" Width="120" VerticalAlignment="Center" />
            </StackPanel>
            <StackPanel Orientation="Horizontal" Width="260">
                <TextBlock Text="PasswordBox: " TextAlignment="Right" Width="80" Height="Auto" VerticalAlignment="Center" />
                <PasswordBox Name="passwordBoxTest" Width="120" VerticalAlignment="Center" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

Event Viewer

Here is the exception from the Event Viewer on the machine:

Application: WPF Control Test.exe
Framework Version: v4.0.30319
Description: The application requested process termination through System.Environment.FailFast(string message).
Message: Unrecoverable system error.
Stack:
at System.Environment.FailFast(System.String)
at MS.Internal.Invariant.FailFast(System.String, System.String)
at System.Windows.Media.FontFamily.get_FirstFontFamily()
at System.Windows.Documents.TextSelection.CalculateCaretRectangle(System.Windows.Documents.ITextSelection, System.Windows.Documents.ITextPointer)
at System.Windows.Documents.TextSelection.UpdateCaretStateWorker(System.Object)
at System.Windows.Documents.TextSelection.UpdateCaretState(System.Windows.Documents.CaretScrollMethod)
at System.Windows.Documents.TextSelection.EnsureCaret(Boolean, System.Windows.Documents.CaretScrollMethod)
at System.Windows.Documents.TextSelection.System.Windows.Documents.ITextSelection.UpdateCaretAndHighlight()
at System.Windows.Documents.TextEditor.OnGotKeyboardFocus(System.Object, System.Windows.Input.KeyboardFocusChangedEventArgs)
at System.Windows.Controls.PasswordBox.OnGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs)
at System.Windows.UIElement.OnGotKeyboardFocusThunk(System.Object, System.Windows.Input.KeyboardFocusChangedEventArgs)
at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(System.Delegate, System.Object)
at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
at System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs, Boolean)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)
at System.Windows.Input.KeyboardDevice.ChangeFocus(System.Windows.DependencyObject, Int32)
at System.Windows.Input.KeyboardDevice.TryChangeFocus(System.Windows.DependencyObject, System.Windows.Input.IKeyboardInputProvider, Boolean, Boolean, Boolean)
at System.Windows.Input.KeyboardDevice.Focus(System.Windows.DependencyObject, Boolean, Boolean, Boolean)
at System.Windows.Input.KeyboardDevice.Focus(System.Windows.IInputElement)
at System.Windows.UIElement.Focus()
at System.Windows.Documents.TextEditorMouse.MoveFocusToUiScope(System.Windows.Documents.TextEditor)
at System.Windows.Documents.TextEditorMouse.OnMouseDown(System.Object, System.Windows.Input.MouseButtonEventArgs)
at System.Windows.Controls.PasswordBox.OnMouseDown(System.Windows.Input.MouseButtonEventArgs)
at System.Windows.UIElement.OnMouseDownThunk(System.Object, System.Windows.Input.MouseButtonEventArgs)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate, System.Object)
at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
at System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
at System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs, Boolean)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs)
at System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr, System.Windows.Input.InputMode, Int32, System.Windows.Input.RawMouseActions, Int32, Int32, Int32)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr, MS.Internal.Interop.WindowMessage, IntPtr, IntPtr, Boolean ByRef)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
at System.Windows.Application.RunDispatcher(System.Object)
at System.Windows.Application.RunInternal(System.Windows.Window)
at System.Windows.Application.Run(System.Windows.Window)
at System.Windows.Application.Run()
at WPF_Control_Test.App.Main()

2
We're going to need to see your code.user7116
Your going to need to see the cause of the error. Since you've said you have installed the application, I'm guessing your not going to get an exception stack trace. As such, you should look in the Windows Event Viewer (type "eventvwr" in the Run dialog and select "Custom Views" > "Administrative Events"). This should allow you to see the stack trace you would normally find inside an exception object.Samuel Slade
There are currently no problems with your XAML, do you have any other C# code-behind related to that PasswordBox.user7116
Thats the only c# code-behind file related to that xaml. The object reference error does not occur in the code above, only in my other application. The line that seems to jump out at me is the one that says System.Windows.Media.FontFamily.get_FirstFontFamily(). Perhaps it is failing when trying to get the font? On the PC a lot of the fonts have been removed. I don't know if this would cause any problems, but it wouldn't make much sense if the TextBox control is working fine.mrstebo

2 Answers

3
votes

It appears that it the .Net Framework is unable to find a suitable default font. I don't know why this is happening, but the following lines of internal code from FontFamily include the line where the exception is being thrown:

fontFamily = this.FindFirstFontFamilyAndFace(
    ref normal, ref normal2, ref normal3);
if (fontFamily == null)
{
    // NullFontFamilyCanonicalName
    //  = CanonicalFontFamilyReference.Create(null, "#ARIAL");
    fontFamily = FontFamily.LookupFontFamily(
        FontFamily.NullFontFamilyCanonicalName);
    Invariant.Assert(fontFamily != null); // <- Your exception
}

Apparently, if you do not specify a font it is going to look for an Arial based font, which it is unable to find on that machine, hence your exception.

1
votes

I had this same problem on a Windows Embedded 7 box, and as mentioned in the link the comments;

http://social.msdn.microsoft.com/Forums/ar/wpf/thread/fc2c9a54-8f66-4f1a-82be-cb40ada5fba5

I simply copied across the following fonts to the "c:\windows\fonts" directory, and the problem was solved;

  • Arial (TrueType)
  • Courier New (TrueType)
  • Times New Roman (TrueType)