Преглед изворни кода

Fix potential for double dispose when saving settings file.

Stephen Damm пре 5 година
родитељ
комит
ad1c3aefb0
1 измењених фајлова са 10 додато и 1 уклоњено
  1. 10 1
      SongBrowserPlugin/DataAccess/SongBrowserSettings.cs

+ 10 - 1
SongBrowserPlugin/DataAccess/SongBrowserSettings.cs

@@ -334,13 +334,22 @@ namespace SongBrowser.DataAccess
                 NewLineHandling = System.Xml.NewLineHandling.Entitize
             };
 
-            using (var stream = new StreamWriter(path, false, Utf8Encoding))
+            // https://docs.microsoft.com/en-gb/visualstudio/code-quality/ca2202-do-not-dispose-objects-multiple-times?view=vs-2015
+            StreamWriter stream = null;
+            try
             {
+                stream = new StreamWriter(path, false, Utf8Encoding);
                 using (var writer = XmlWriter.Create(stream, settings))
                 {
+                    stream = null;
                     SettingsSerializer.Serialize(writer, this);
                 }
             }
+            finally
+            {
+                if (stream != null)
+                    stream.Dispose();
+            }
         }
     }
 }