[lightsoff/wip/gtkview] Simplified code and reduced GTK memleak



commit dbe515a1c3f6a97eb985bad58789a8c920ec1fca
Author: Robert Roth <robert roth off gmail com>
Date:   Tue Jul 17 00:44:27 2018 +0300

    Simplified code and reduced GTK memleak

 src/game-view-clutter.vala | 45 +++++++++++++++++++--------------------------
 src/game-view-gtk.vala     | 28 +++++++++++++++++-----------
 src/game-view.vala         |  2 +-
 3 files changed, 37 insertions(+), 38 deletions(-)
---
diff --git a/src/game-view-clutter.vala b/src/game-view-clutter.vala
index cd32bf0..6e1be04 100644
--- a/src/game-view-clutter.vala
+++ b/src/game-view-clutter.vala
@@ -26,7 +26,6 @@ public class ClutterGameView : Clutter.Group, GameView
 
     private Clutter.Actor board_group;
     private BoardViewClutter board_view;
-    private BoardViewClutter? new_board_view = null;
     private Clutter.Actor key_cursor_view;
 
     private Clutter.Timeline timeline;
@@ -106,18 +105,17 @@ public class ClutterGameView : Clutter.Group, GameView
     }
 
     // The boards have finished transitioning; delete the old one!
-    private void transition_complete_cb ()
+    private void board_replaced (BoardViewClutter old_view, BoardViewClutter new_view)
     {
-        board_view.destroy ();
-        board_view = new_board_view;
-        board_view.playable = true;
-        new_board_view = null;
-        timeline = null;
-
+        old_view.destroy ();
         // Remove all of the queued-for-removal actors
         foreach (var actor in actor_remove_queue)
             actor.destroy ();
         actor_remove_queue = null;
+
+        new_view.playable = true;
+        timeline = null;
+        board_view = new_view;
     }
 
     private void light_toggled_cb ()
@@ -132,8 +130,7 @@ public class ClutterGameView : Clutter.Group, GameView
         if (timeline != null && timeline.is_playing ())
             return;
 
-        board_view = replace_board (board_view, create_board_view (++current_level), 
GameView.ReplaceStyle.SLIDE_NEXT) as BoardViewClutter;
-        level_changed (current_level);
+        replace_board (board_view, create_board_view (++current_level), GameView.ReplaceStyle.SLIDE_NEXT);
     }
 
     // The player asked to swap to a different level without completing
@@ -152,26 +149,26 @@ public class ClutterGameView : Clutter.Group, GameView
             return;
         }
 
-        board_view = replace_board (board_view, create_board_view (current_level),
+        replace_board (board_view, create_board_view (current_level),
                        direction == 1 ? GameView.ReplaceStyle.SLIDE_FORWARD 
-                                      : GameView.ReplaceStyle.SLIDE_BACKWARD) as BoardViewClutter;
+                                      : GameView.ReplaceStyle.SLIDE_BACKWARD);
 
-        level_changed (current_level);
     }
 
-    public BoardView replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, 
bool fast = true)
+    public void replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, bool 
fast = true)
     {
-        new_board_view = new_board as BoardViewClutter;
         timeline = new Clutter.Timeline (fast ? 500 : 1500);
         board_group.add_child (new_board as Clutter.Group);
         int direction = 1;
+        BoardViewClutter new_board_view = new_board as BoardViewClutter;
+        BoardViewClutter old_board_view = old_board as BoardViewClutter;
         switch (style)
         {
             case REFRESH: 
                 new_board_view.z_position = 250;
                 new_board_view.opacity = 0;
                 /* Fade into background or drop down */
-                board_view.animate_with_timeline (Clutter.AnimationMode.EASE_IN_SINE, timeline,
+                old_board_view.animate_with_timeline (Clutter.AnimationMode.EASE_IN_SINE, timeline,
                                        "z_position", 250.0 * -1,
                                        "opacity", 0);
                         /* Bring into foreground and make visible */
@@ -194,13 +191,13 @@ public class ClutterGameView : Clutter.Group, GameView
 
                 timeline = new Clutter.Timeline (1500);
                 new_board_view.slide_in (direction, sign, timeline);
-                board_view.slide_out (direction, sign, timeline);
+                old_board_view.slide_out (direction, sign, timeline);
                 break;
             case SLIDE_FORWARD: 
                 new_board_view.z_position = -250 * direction;
                 new_board_view.opacity = 0;
                 /* Fade into background or drop down */
-                board_view.animate_with_timeline (Clutter.AnimationMode.EASE_IN_SINE, timeline,
+                old_board_view.animate_with_timeline (Clutter.AnimationMode.EASE_IN_SINE, timeline,
                                        "z_position", 250.0 * direction,
                                        "opacity", 0);
                         /* Bring into foreground and make visible */
@@ -213,7 +210,7 @@ public class ClutterGameView : Clutter.Group, GameView
                 new_board_view.z_position = -250 * direction;
                 new_board_view.opacity = 0;
                 /* Fade into background or drop down */
-                board_view.animate_with_timeline (Clutter.AnimationMode.EASE_IN_SINE, timeline,
+                old_board_view.animate_with_timeline (Clutter.AnimationMode.EASE_IN_SINE, timeline,
                                        "z_position", 250.0 * direction,
                                        "opacity", 0);
                         /* Bring into foreground and make visible */
@@ -224,12 +221,10 @@ public class ClutterGameView : Clutter.Group, GameView
 
             default: break;
         } 
-
-        timeline.completed.connect (transition_complete_cb);
-        return new_board;
+        timeline.completed.connect (() => board_replaced (old_board_view, new_board_view));
+        level_changed (current_level);
     }
 
-
     public bool hide_cursor ()
     {
         setup_animation (key_cursor_view, Clutter.AnimationMode.EASE_OUT_SINE, 250);
@@ -284,8 +279,6 @@ public class ClutterGameView : Clutter.Group, GameView
 
         current_level = 1;
 
-        board_view = replace_board (board_view, create_board_view (current_level), 
GameView.ReplaceStyle.REFRESH) as BoardViewClutter;
-
-        level_changed (current_level);
+        replace_board (board_view, create_board_view (current_level), GameView.ReplaceStyle.REFRESH);
     }
 }
diff --git a/src/game-view-gtk.vala b/src/game-view-gtk.vala
index 2e96769..2cf0f24 100644
--- a/src/game-view-gtk.vala
+++ b/src/game-view-gtk.vala
@@ -2,18 +2,18 @@ public class GtkGameView : Gtk.Stack, GameView {
 
     private BoardViewGtk board_view;
     private int current_level;
-
+    private GLib.Queue<ulong> handlers = new GLib.Queue<ulong>();
     public void swap_board (int direction)
     {
         current_level += direction;
-        board_view = replace_board (board_view, create_board_view (current_level),
+        replace_board (board_view, create_board_view (current_level),
                        direction == 1 ? GameView.ReplaceStyle.SLIDE_FORWARD 
-                                      : GameView.ReplaceStyle.SLIDE_BACKWARD) as BoardViewGtk;
-        level_changed (current_level);
+                                      : GameView.ReplaceStyle.SLIDE_BACKWARD);
     }
 
-    public BoardView replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, 
bool fast = true)
+    public void replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, bool 
fast = true)
     {
+        stdout.printf ("Changing board %p with board %p\n", old_board, new_board);
         transition_duration = fast ? 500 : 1000;
         switch (style)
         {
@@ -32,8 +32,16 @@ public class GtkGameView : Gtk.Stack, GameView {
         var new_level = "level %d".printf(current_level);
         add_named (new_board as Gtk.Widget, new_level);
         set_visible_child (new_board as Gtk.Widget);
-        notify["transition-running"].connect(() => remove (old_board as Gtk.Widget));
-        return new_board;
+        handlers.push_tail(notify["transition-running"].connect(() => board_replaced (old_board as 
BoardViewGtk, new_board as BoardViewGtk)));
+        level_changed (current_level);
+    }
+
+    public void board_replaced (BoardViewGtk old_board, BoardViewGtk new_board)
+    {
+        stdout.printf ("Cleaning board %p, replacing with  %p\n", old_board, new_board);
+        @foreach((board) => { if (board != get_visible_child ()) remove(board);});
+        board_view = new_board;
+        disconnect(handlers.pop_head());
     }
 
     public bool hide_cursor ()
@@ -53,8 +61,7 @@ public class GtkGameView : Gtk.Stack, GameView {
     public void reset_game ()
     {
         current_level = 1;
-        board_view = replace_board (board_view, create_board_view (current_level), 
GameView.ReplaceStyle.REFRESH) as BoardViewGtk;
-        level_changed (current_level);
+        replace_board (board_view, create_board_view (current_level), GameView.ReplaceStyle.REFRESH);
     }
 
     public GtkGameView (int level)
@@ -87,8 +94,7 @@ public class GtkGameView : Gtk.Stack, GameView {
     // and transition between the two boards in a random direction.
     private bool game_won_cb ()
     {
-        board_view = replace_board (board_view, create_board_view (++current_level), 
GameView.ReplaceStyle.SLIDE_NEXT, false) as BoardViewGtk;
-        level_changed (current_level);
+        replace_board (board_view, create_board_view (++current_level), GameView.ReplaceStyle.SLIDE_NEXT, 
false);
         return false;
     }
 
diff --git a/src/game-view.vala b/src/game-view.vala
index f8909df..3dfb0c9 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -18,7 +18,7 @@ public interface GameView : GLib.Object {
 
     public abstract void swap_board (int direction);
 
-    public abstract BoardView replace_board (BoardView board_biew, BoardView new_board_view, ReplaceStyle 
style, bool fast = true);
+    public abstract void replace_board (BoardView board_biew, BoardView new_board_view, ReplaceStyle style, 
bool fast = true);
 
     public abstract bool hide_cursor ();
     public abstract bool activate_cursor ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]