changeset 199:70ba3bee0c2e

Fixes #207: WarFoundry still keeps file handles on failure * Move close of zip file into a "finally" block * Remove unnecessary extra Close()
author IBBoard <dev@ibboard.co.uk>
date Mon, 02 Nov 2009 20:58:01 +0000
parents c4cf4c7db7d5
children 3e287b6b5244
files api/Factories/AbstractNativeWarFoundryFactory.cs api/Factories/Xml/WarFoundryXmlFactory.cs
diffstat 2 files changed, 27 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Oct 31 20:55:19 2009 +0000
+++ b/api/Factories/AbstractNativeWarFoundryFactory.cs	Mon Nov 02 20:58:01 2009 +0000
@@ -82,19 +82,26 @@
 		{
 			ICollection<IWarFoundryObject> objects = null;
 			string comment = file.ZipFileComment;
-			IWarFoundryObject obj = null;
-				
-			if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER))
-			{
-				obj = CreateGameSystemFromFile(file);
-			}
-			else if (comment.StartsWith(RACE_ZIP_IDENTIFIER))
-			{
-				obj = CreateRaceFromFile(file);
-			}
-			else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER))
-			{
-				obj = CreateArmyFromFile(file);
+			IWarFoundryObject obj = null;
+
+			try
+			{
+				if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER))
+				{
+					obj = CreateGameSystemFromFile(file);
+				}
+				else if (comment.StartsWith(RACE_ZIP_IDENTIFIER))
+				{
+					obj = CreateRaceFromFile(file);
+				}
+				else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER))
+				{
+					obj = CreateArmyFromFile(file);
+				}
+			}
+			finally
+			{
+				file.Close();
 			}
 			
 			if (obj!=null)
@@ -102,8 +109,6 @@
 				objects = new List<IWarFoundryObject>();
 				objects.Add(obj);
 			}
-
-			file.Close();
 			
 			return objects;
 		}
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Oct 31 20:55:19 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Mon Nov 02 20:58:01 2009 +0000
@@ -76,7 +76,7 @@
 		private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName)
 		{
 			XmlDocument doc = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream);
-			stream.Close();
+
 			XmlElement elem = (XmlElement)doc.LastChild;
 			
 			if (!elem.LocalName.Equals(elementName.Value))
@@ -108,12 +108,12 @@
 		{
 			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
 			LogNotifier.Debug(GetType(), "Create Race");
-			return raceFactory.CreateRaceFromElement(file, elem);
-		}
-
-		protected override void CleanUpFileAsSupportedType(ZipFile typedFile)
-		{
-			typedFile.Close();
+			return raceFactory.CreateRaceFromElement(file, elem);
+		}
+
+		protected override void CleanUpFileAsSupportedType(ZipFile typedFile)
+		{
+			typedFile.Close();
 		}
 
 		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)