Browse Source

more dispose

Charlie Kindel 5 years ago
parent
commit
4a4ff7fc7a

+ 14 - 0
Terminal.Gui/Core/Responder.cs

@@ -14,6 +14,7 @@
 //   - Add rendering limitation to the exposed area
 
 using System;
+using System.Collections.Generic;
 
 namespace Terminal.Gui {
 	/// <summary>
@@ -22,6 +23,18 @@ namespace Terminal.Gui {
 	public class Responder : IDisposable {
 		bool disposedValue;
 
+#if DEBUG
+		/// <summary>
+		/// For debug purposes to verify objects are being disposed properly
+		/// </summary>
+		public bool WasDisposed = false;
+		public static List<Responder> Instances = new List<Responder> ();
+		public Responder ()
+		{
+			Instances.Add (this);
+		}
+#endif 
+
 		/// <summary>
 		/// Gets or sets a value indicating whether this <see cref="Responder"/> can focus.
 		/// </summary>
@@ -219,6 +232,7 @@ namespace Terminal.Gui {
 			// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
 			Dispose (disposing: true);
 			GC.SuppressFinalize (this);
+			WasDisposed = true;
 		}
 	}
 }

+ 7 - 0
Terminal.Gui/Views/ScrollView.cs

@@ -635,5 +635,12 @@ namespace Terminal.Gui {
 			}
 			return true;
 		}
+
+		protected override void Dispose (bool disposing)
+		{
+			vertical?.Dispose ();
+			horizontal?.Dispose ();
+			base.Dispose (disposing);
+		}
 	}
 }

+ 16 - 5
UICatalog/Scenarios/CharacterMap.cs

@@ -15,9 +15,10 @@ namespace UICatalog {
 	[ScenarioCategory ("Text")]
 	[ScenarioCategory ("Controls")]
 	class CharacterMap : Scenario {
+		CharMap _charMap;
 		public override void Setup ()
 		{
-			var charMap = new CharMap () {
+			_charMap = new CharMap () {
 				X = 0,
 				Y = 0,
 				Width = CharMap.RowWidth + 2,
@@ -26,8 +27,8 @@ namespace UICatalog {
 				ColorScheme = Colors.Dialog
 			};
 
-			Win.Add (charMap);
-			var label = new Label ("Jump To Unicode Block:") { X = Pos.Right (charMap) + 1, Y = Pos.Y (charMap) };
+			Win.Add (_charMap);
+			var label = new Label ("Jump To Unicode Block:") { X = Pos.Right (_charMap) + 1, Y = Pos.Y (_charMap) };
 			Win.Add (label);
 
 			(ustring radioLabel, int start, int end) CreateRadio (ustring title, int start, int end)
@@ -57,11 +58,17 @@ namespace UICatalog {
 			jumpList.Y = Pos.Bottom (label);
 			jumpList.Width = Dim.Fill ();
 			jumpList.SelectedItemChanged = (args) => {
-				charMap.Start = radioItems[args.SelectedItem].start;
+				_charMap.Start = radioItems[args.SelectedItem].start;
 			};
 
 			Win.Add (jumpList);
 		}
+
+		public override void Run ()
+		{
+			base.Run ();
+			_charMap.Dispose ();
+		}
 	}
 
 	class CharMap : ScrollView {
@@ -101,7 +108,6 @@ namespace UICatalog {
 
 			DrawContent += CharMap_DrawContent;
 		}
-
 #if true
 		private void CharMap_DrawContent (Rect viewport)
 		{
@@ -122,6 +128,11 @@ namespace UICatalog {
 				}
 			}
 		}
+
+		protected override void Dispose (bool disposing)
+		{
+			base.Dispose (disposing);
+		}
 #else
 		public override void OnDrawContent (Rect viewport)
 		{

+ 2 - 0
UICatalog/Scenarios/TopLevelNoWindowBug.cs

@@ -8,6 +8,8 @@ namespace UICatalog {
 
 		public override void Run ()
 		{
+			Top?.Dispose ();
+
 			Top = new Toplevel (new Rect (0, 0, Application.Driver.Cols, Application.Driver.Rows));
 
 			var menu = new MenuBar (new MenuBarItem [] {

+ 10 - 1
UnitTests/ScenarioTests.cs

@@ -71,6 +71,10 @@ namespace Terminal.Gui {
 				Assert.Equal (1, iterations);
 				Assert.Equal (stackSize, iterations);
 			}
+
+			foreach (var inst in Responder.Instances) {
+				Assert.True (inst.WasDisposed);
+			}
 		}
 
 		[Fact]
@@ -79,7 +83,7 @@ namespace Terminal.Gui {
 			List<Type> scenarioClasses = Scenario.GetDerivedClasses<Scenario> ();
 			Assert.NotEmpty (scenarioClasses);
 
-			var item = scenarioClasses.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals ("Generic", StringComparison.OrdinalIgnoreCase));
+			var item = scenarioClasses.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals ("Clipping", StringComparison.OrdinalIgnoreCase));
 			var scenarioClass = scenarioClasses[item];
 			// Setup some fake kepresses 
 			// Passing empty string will cause just a ctrl-q to be fired
@@ -119,6 +123,11 @@ namespace Terminal.Gui {
 			// # of key up events should match # of iterations
 			//Assert.Equal (1, iterations);
 			Assert.Equal (stackSize, iterations);
+
+
+			foreach (var inst in Responder.Instances) {
+				Assert.True (inst.WasDisposed);
+			}
 		}
 	}
 }