From b5002cb89d659c932240816b5f8bbe8b28482d38 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 16 Jul 2020 09:26:21 +0800 Subject: [PATCH] Fix crash due to load invalid preference.xml --- SourceGit/App.xaml | 3 +-- SourceGit/App.xaml.cs | 10 +--------- SourceGit/Git/Preference.cs | 12 ++++++++---- SourceGit/Git/Repository.cs | 1 - 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/SourceGit/App.xaml b/SourceGit/App.xaml index 875ead33..408df2f0 100644 --- a/SourceGit/App.xaml +++ b/SourceGit/App.xaml @@ -2,8 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="OnAppStartup" - Deactivated="OnAppDeactivated" - Exit="OnAppExit"> + Deactivated="OnAppDeactivated"> diff --git a/SourceGit/App.xaml.cs b/SourceGit/App.xaml.cs index a5f6b39c..e0ebeac8 100644 --- a/SourceGit/App.xaml.cs +++ b/SourceGit/App.xaml.cs @@ -119,16 +119,8 @@ namespace SourceGit { /// /// private void OnAppDeactivated(object sender, EventArgs e) { - GC.Collect(); - } - - /// - /// Quit event. - /// - /// - /// - private void OnAppExit(object sender, ExitEventArgs e) { Git.Preference.Save(); + GC.Collect(); } } } diff --git a/SourceGit/Git/Preference.cs b/SourceGit/Git/Preference.cs index 2d223b53..092462fe 100644 --- a/SourceGit/Git/Preference.cs +++ b/SourceGit/Git/Preference.cs @@ -134,10 +134,14 @@ namespace SourceGit.Git { return; } - var stream = new FileStream(SAVE_PATH, FileMode.Open); - var reader = new XmlSerializer(typeof(Preference)); - instance = (Preference)reader.Deserialize(stream); - stream.Close(); + try { + var stream = new FileStream(SAVE_PATH, FileMode.Open); + var reader = new XmlSerializer(typeof(Preference)); + instance = (Preference)reader.Deserialize(stream); + stream.Close(); + } catch { + instance = new Preference(); + } } /// diff --git a/SourceGit/Git/Repository.cs b/SourceGit/Git/Repository.cs index 46db6447..161ea027 100644 --- a/SourceGit/Git/Repository.cs +++ b/SourceGit/Git/Repository.cs @@ -4,7 +4,6 @@ using System.Diagnostics; using System.IO; using System.Text; using System.Text.RegularExpressions; -using System.Windows.Forms; using System.Windows.Threading; using System.Xml.Serialization;