MaterialDesignInXAML / MaterialDesignInXAML/MaterialDesignInXamlToolkit

DialogHost can throw exception when opening and closing dialog in quick succession

Offen
#1,054 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug evaluation required
Vorherrschende Sprache
C#
Sterne
16.3k
Forks
3.5k
Ø Merge
1 T. 22 Std.
Gemergte PRs (30 T.)
8

Beschreibung

DialogHost can throw exception when opening and closing dialog in quick succession.

## Background

I have a view which is password protected. The password dialog is shown when the user opens the view. The dialog is using a `DialogHost` placed on the view and is the only "client" of the dialog host. If the user navigates away from the view, the dialog will be closed.

## Problem

If the dialog is shown and the user navigates away from the view shortly after there is a chance that the `DialogHost` will throw a `NullReferenceException`.

```
2018-10-04 09:02:10.870 +02:00 [Error ] [02] Unhandled exception occurred. Origin=System.Threading.Tasks.TaskScheduler.UnobservedTaskException,
System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at MaterialDesignThemes.Wpf.DialogHost.<>c__DisplayClass38_1.b__2(Task t) in C:\projects\materialdesigninxamltoolkit\MaterialDesignThemes.Wpf\DialogHost.cs:line 296
at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
at MaterialDesignThemes.Wpf.DialogHost.<>c__DisplayClass38_1.b__2(Task t) in C:\projects\materialdesigninxamltoolkit\MaterialDesignThemes.Wpf\DialogHost.cs:line 296
at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()<---
```

My guess is that since there is a delay when opening a dialog it is possible that some reference become `null` during the delay. I think that the `child` variable should not change value but it could be possible that its `Dispatcher` property becomes `null` when the object is removed from visual tree or something like that.

Anyway, the code might need some more protective `null` checking.

```
dialogHost.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
CommandManager.InvalidateRequerySuggested();
UIElement child = dialogHost.FocusPopup();

if (child != null)
{
//https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/issues/187
//totally not happy about this, but on immediate validation we can get some weird looking stuff...give WPF a kick to refresh...
Task.Delay(300).ContinueWith(t => child.Dispatcher.BeginInvoke(new Action(() => child.InvalidateVisual())));
}
}));
```

see [DialogHost.cs](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/master/MaterialDesignThemes.Wpf/DialogHost.cs#L289-L300)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie in MaterialDesignThemes.Wpf/DialogHost.cs bei den Zeilen 289–300, insbesondere bei der verzögerten Fortsetzung in IsOpenPropertyChangedCallback. Reproduzieren Sie eine schnelle Sequenz zum Öffnen und Schließen oder zum Wegnavigieren und verfolgen Sie, welche Referenz während der Verzögerung nicht mehr verfügbar sein kann. Als abgeschlossen gilt die Änderung, wenn die Sequenz nicht mehr die gemeldete NullReferenceException oder eine nicht beobachtete Task-Ausnahme erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
desktop
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.