From 4a4ff7fc7a6b489933ee82ed29e85ba73221cccf Mon Sep 17 00:00:00 2001 From: Charlie Kindel Date: Sat, 13 Jun 2020 16:10:48 -0700 Subject: [PATCH] more dispose --- Terminal.Gui/Core/Responder.cs | 14 ++++++++++++++ Terminal.Gui/Views/ScrollView.cs | 7 +++++++ UICatalog/Scenarios/CharacterMap.cs | 21 ++++++++++++++++----- UICatalog/Scenarios/TopLevelNoWindowBug.cs | 2 ++ UnitTests/ScenarioTests.cs | 11 ++++++++++- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Terminal.Gui/Core/Responder.cs b/Terminal.Gui/Core/Responder.cs index 53bdf3476..9aeb212fd 100644 --- a/Terminal.Gui/Core/Responder.cs +++ b/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 { /// @@ -22,6 +23,18 @@ namespace Terminal.Gui { public class Responder : IDisposable { bool disposedValue; +#if DEBUG + /// + /// For debug purposes to verify objects are being disposed properly + /// + public bool WasDisposed = false; + public static List Instances = new List (); + public Responder () + { + Instances.Add (this); + } +#endif + /// /// Gets or sets a value indicating whether this can focus. /// @@ -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; } } } diff --git a/Terminal.Gui/Views/ScrollView.cs b/Terminal.Gui/Views/ScrollView.cs index 7bfdbd5fb..278ecf1f2 100644 --- a/Terminal.Gui/Views/ScrollView.cs +++ b/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); + } } } diff --git a/UICatalog/Scenarios/CharacterMap.cs b/UICatalog/Scenarios/CharacterMap.cs index 60d30ee83..0d9fed5f0 100644 --- a/UICatalog/Scenarios/CharacterMap.cs +++ b/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) { diff --git a/UICatalog/Scenarios/TopLevelNoWindowBug.cs b/UICatalog/Scenarios/TopLevelNoWindowBug.cs index 47bd42dc1..88a87c92f 100644 --- a/UICatalog/Scenarios/TopLevelNoWindowBug.cs +++ b/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 [] { diff --git a/UnitTests/ScenarioTests.cs b/UnitTests/ScenarioTests.cs index 193dfd020..4bef52740 100644 --- a/UnitTests/ScenarioTests.cs +++ b/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 scenarioClasses = Scenario.GetDerivedClasses (); 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); + } } } }