VOID_DataLogger: Finish writing and close the file when we're destructed.
VOID_DataLogger: Finish writing and close the file when we're destructed.

--- a/VOID_DataLogger.cs
+++ b/VOID_DataLogger.cs
@@ -56,6 +56,8 @@
 
 		protected System.Text.UTF8Encoding utf8Encoding;
 		protected FileStream _outputFile;
+
+		protected uint outstandingWrites;
 
 		protected List<string> csvList = new List<string>();
 
@@ -161,6 +163,7 @@
 			this.csv_log_interval_str = "0.5";
 
 			this.csvCollectTimer = 0;
+			this.outstandingWrites = 0;
 
 			this.WindowPos.x = Screen.width - 520;
 			this.WindowPos.y = 85;
@@ -288,6 +291,11 @@
 				this.AsyncWriteData();
 			}
 
+			while (this.outstandingWrites > 0)
+			{
+				System.Threading.Thread.Sleep(10);
+			}
+
 			if (this._outputFile != null)
 			{
 				logger.Append(" Closing File...");
@@ -298,11 +306,17 @@
 			logger.Print();
 		}
 
+		~VOID_DataLogger()
+		{
+			this.OnDestroy();
+		}
+
 		protected void AsyncWriteCallback(IAsyncResult result)
 		{
 			Tools.PostDebugMessage(this, "Got async callback, IsCompleted = {0}", result.IsCompleted);
 
 			this.outputFile.EndWrite(result);
+			this.outstandingWrites--;
 		}
 
 		private void AsyncWriteData()
@@ -323,7 +337,9 @@
 			WriteState state = new WriteState();
 
 			state.bytes = bytes.ToArray();
-
+			state.stream = this.outputFile;
+
+			this.outstandingWrites++;
 			var writeCallback = new AsyncCallback(this.AsyncWriteCallback);
 
 			this.outputFile.BeginWrite(state.bytes, 0, state.bytes.Length, writeCallback, state);
@@ -433,7 +449,7 @@
 		private class WriteState
 		{
 			public byte[] bytes;
-			public KSP.IO.FileStream stream;
+			public FileStream stream;
 		}
 	}
 }