Why does the PasswordBox control crash my WPF application?

I spent a good day trying to understand why PasswordBox is causing me so many problems.
I have an application that runs on every machine on which I tested it. Now it was installed on other machines that it crashes when I click inside PasswordBox .
I even made a test application using only the TextBox and PasswordBox controls to find out if the crash was caused by some basic code in my application, but this turned out to be wrong. I click on TextBox and it works great. Allows me to enter text inside it. But as soon as I click on PasswordBox , the application crashes.

I set 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 that there were no errors in these events.
I installed some entries in my application and I have exceptions, for example:

  • Object reference not set to object instance
  • Attempted to read or write protected memory. This often indicates that another memory is corrupted.

I set an event for PasswordBox to check if it had focus before it crashed, and that happened, so I don't think that the Object link exception refers to the PasswordBox control.
I searched everywhere to find out if anyone has any other problems, and only problems with the TextBox control and its problems with focus, which should be fixed in .NET 4.0

ABOUT! and it reminds me. I am using WPF.NET 4.0.

Any help would be greatly appreciated.

Security Code:

 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 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 to the event viewer on the computer:

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

+4
source share
2 answers

It seems that the .Net Framework cannot 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 in which the exception is 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 will look for the Arial font, which it cannot find on this computer, therefore, your exception.

+3
source

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

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

I just copied the following fonts to the "c: \ windows \ fonts" directory and the problem was resolved;

  • Arial (TrueType)
  • Courier New (TrueType)
  • Times New Roman (TrueType)
+1
source

Source: https://habr.com/ru/post/1390449/


All Articles