[Xfce4-commits] [panel-plugins/xfce4-pulseaudio-plugin] 13/21: Memory management for MprisMenuItem

noreply at xfce.org noreply at xfce.org
Sat Sep 23 21:27:39 CEST 2017


This is an automated email from the git hooks/post-receive script.

b   l   u   e   s   a   b   r   e       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository panel-plugins/xfce4-pulseaudio-plugin.

commit 55c3352d223110c28afb3c0a23faa6d9cb7debea
Author: Sean Davis <smd.seandavis at gmail.com>
Date:   Tue Sep 12 06:48:22 2017 -0400

    Memory management for MprisMenuItem
---
 panel-plugin/devicemenuitem.c |  14 ++++-
 panel-plugin/mprismenuitem.c  | 134 +++++++++++++++++++++++++++---------------
 panel-plugin/scalemenuitem.c  |  11 +++-
 3 files changed, 105 insertions(+), 54 deletions(-)

diff --git a/panel-plugin/devicemenuitem.c b/panel-plugin/devicemenuitem.c
index 6f86be2..db09ea5 100644
--- a/panel-plugin/devicemenuitem.c
+++ b/panel-plugin/devicemenuitem.c
@@ -111,8 +111,12 @@ device_menu_item_finalize (GObject *object)
   self = DEVICE_MENU_ITEM (object);
   priv = GET_PRIVATE (self);
 
-  g_free (priv->title);
-  priv->title = NULL;
+  if (priv->title)
+    g_free (priv->title);
+
+  g_object_unref (priv->submenu);
+  g_object_unref (priv->image);
+  g_object_unref (priv->label);
 
   (*G_OBJECT_CLASS (device_menu_item_parent_class)->finalize) (object);
 }
@@ -150,6 +154,11 @@ G_GNUC_END_IGNORE_DEPRECATIONS
   /* Configure menu item submenu */
   gtk_menu_item_set_submenu (GTK_MENU_ITEM (device_item), priv->submenu);
 
+  /* Keep references to the widgets */
+  g_object_ref (priv->submenu);
+  g_object_ref (priv->image);
+  g_object_ref (priv->label);
+
   return GTK_WIDGET(device_item);
 }
 
@@ -233,5 +242,4 @@ device_menu_item_set_image_from_icon_name (DeviceMenuItem *item,
   priv = GET_PRIVATE (item);
 
   gtk_image_set_from_icon_name (GTK_IMAGE (priv->image), icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
-  gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 24);
 }
diff --git a/panel-plugin/mprismenuitem.c b/panel-plugin/mprismenuitem.c
index 61a9ac4..1f1c7c4 100644
--- a/panel-plugin/mprismenuitem.c
+++ b/panel-plugin/mprismenuitem.c
@@ -38,11 +38,12 @@
 #include <libxfce4util/libxfce4util.h>
 
 
-static gboolean mpris_menu_item_button_press_event      (GtkWidget          *menuitem,
-                                                         GdkEventButton     *event);
-static gboolean mpris_menu_item_button_release_event    (GtkWidget          *menuitem,
-                                                         GdkEventButton     *event);
-static void     update_packing                          (MprisMenuItem      *self);
+static gboolean mpris_menu_item_button_press_event      (GtkWidget       *menuitem,
+                                                         GdkEventButton  *event);
+static gboolean mpris_menu_item_button_release_event    (GtkWidget       *menuitem,
+                                                         GdkEventButton  *event);
+static void     update_packing                          (MprisMenuItem   *self);
+static void     mpris_menu_item_finalize                (GObject         *object);
 
 
 
@@ -68,6 +69,7 @@ struct _MprisMenuItemPrivate {
   gchar     *title;
   gchar     *filename;
 
+  GtkWidget *image;
   GtkWidget *vbox;
   GtkWidget *hbox;
   GtkWidget *button_box;
@@ -91,8 +93,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
 static void
 mpris_menu_item_class_init (MprisMenuItemClass *item_class)
 {
+  GObjectClass      *gobject_class =   G_OBJECT_CLASS      (item_class);
   GtkWidgetClass    *widget_class =    GTK_WIDGET_CLASS    (item_class);
 
+  gobject_class->finalize = mpris_menu_item_finalize;
   widget_class->button_press_event   = mpris_menu_item_button_press_event;
   widget_class->button_release_event = mpris_menu_item_button_release_event;
 
@@ -115,21 +119,6 @@ mpris_menu_item_class_init (MprisMenuItemClass *item_class)
 }
 
 static void
-remove_children (GtkContainer *container)
-{
-  GList * children;
-  GList * l;
-
-  g_return_if_fail (GTK_IS_CONTAINER (container));
-
-  children = gtk_container_get_children (container);
-
-  for (l=children; l != NULL; l=l->next)
-    gtk_container_remove (container, l->data);
-  g_list_free (children);
-}
-
-static void
 gtk_label_set_markup_printf_escaped (GtkLabel    *label,
                                      const gchar *format,
                                      ...)
@@ -260,6 +249,10 @@ update_packing (MprisMenuItem *self)
   g_return_if_fail (IS_MPRIS_MENU_ITEM (self));
 
   priv = GET_PRIVATE (self);
+
+  TRACE("entering");
+
+  /* Create the widgets */
   hbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
   vbox = GTK_BOX (gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
 
@@ -267,36 +260,25 @@ update_packing (MprisMenuItem *self)
   ctx = gtk_widget_get_style_context (GTK_WIDGET (button_box));
   gtk_style_context_add_class (ctx, "linked");
 
-  TRACE("entering");
-
-  if (priv->vbox)
-    remove_children (GTK_CONTAINER (priv->vbox));
-  if (priv->hbox)
-  {
-    remove_children (GTK_CONTAINER (priv->hbox));
-    gtk_container_remove (GTK_CONTAINER (self), priv->hbox);
-  }
-
   priv->hbox = GTK_WIDGET(hbox);
   priv->vbox = GTK_WIDGET(vbox);
   priv->button_box = GTK_WIDGET(button_box);
 
-  /* add the new layout */
-  /* [IC]  Title   [  CON  ]
-   * [ON]  Artist  [ TROLS ]
-   */
   priv->go_previous = gtk_button_new_from_icon_name ("media-skip-backward-symbolic", GTK_ICON_SIZE_MENU);
   priv->play_pause = gtk_button_new_from_icon_name ("media-playback-start-symbolic", GTK_ICON_SIZE_MENU);
   priv->go_next = gtk_button_new_from_icon_name ("media-skip-forward-symbolic", GTK_ICON_SIZE_MENU);
 
-  g_signal_connect (priv->play_pause, "clicked", G_CALLBACK (media_play_pause_clicked_event), self);
-  g_signal_connect (priv->go_previous, "clicked", G_CALLBACK (media_go_previous_clicked_event), self);
-  g_signal_connect (priv->go_next, "clicked", G_CALLBACK (media_go_next_clicked_event), self);
-  g_signal_connect (self, "activate", G_CALLBACK (menu_item_activate_event), self);
-
   priv->title_label = track_info_label_new ();
   priv->artist_label = track_info_label_new ();
 
+  priv->image = gtk_image_new_from_icon_name ("audio-x-generic", GTK_ICON_SIZE_LARGE_TOOLBAR);
+
+  /* Pack the widgets */
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (self), priv->image);
+G_GNUC_END_IGNORE_DEPRECATIONS
+  gtk_image_set_pixel_size (GTK_IMAGE (priv->image), 24);
+
   gtk_box_pack_start (button_box, priv->go_previous, FALSE, FALSE, 0);
   gtk_box_pack_start (button_box, priv->play_pause, FALSE, FALSE, 0);
   gtk_box_pack_start (button_box, priv->go_next, FALSE, FALSE, 0);
@@ -307,9 +289,27 @@ update_packing (MprisMenuItem *self)
   gtk_box_pack_start (hbox, GTK_WIDGET (vbox), TRUE, TRUE, 0);
   gtk_box_pack_start (hbox, GTK_WIDGET (button_box), FALSE, FALSE, 0);
 
+  /* Configure the widgets */
   mpris_menu_item_set_title (self, priv->player);
   mpris_menu_item_set_artist (self, _("Not currently playing"));
 
+  g_signal_connect (priv->play_pause, "clicked", G_CALLBACK (media_play_pause_clicked_event), self);
+  g_signal_connect (priv->go_previous, "clicked", G_CALLBACK (media_go_previous_clicked_event), self);
+  g_signal_connect (priv->go_next, "clicked", G_CALLBACK (media_go_next_clicked_event), self);
+  g_signal_connect (self, "activate", G_CALLBACK (menu_item_activate_event), self);
+
+  /* Keep a reference to each */
+  g_object_ref (priv->title_label);
+  g_object_ref (priv->artist_label);
+  g_object_ref (priv->button_box);
+  g_object_ref (priv->vbox);
+  g_object_ref (priv->hbox);
+  g_object_ref (priv->go_previous);
+  g_object_ref (priv->play_pause);
+  g_object_ref (priv->go_next);
+  g_object_ref (priv->image);
+
+  /* And finally display them */
   gtk_widget_show_all (priv->button_box);
   gtk_widget_show_all (priv->hbox);
   gtk_widget_show_all (priv->vbox);
@@ -320,6 +320,51 @@ update_packing (MprisMenuItem *self)
 static void
 mpris_menu_item_init (MprisMenuItem *self)
 {
+  MprisMenuItemPrivate *priv;
+
+  priv = GET_PRIVATE (self);
+
+  priv->title_label = NULL;
+  priv->artist_label = NULL;
+  priv->button_box = NULL;
+  priv->vbox = NULL;
+  priv->hbox = NULL;
+  priv->go_previous = NULL;
+  priv->play_pause = NULL;
+  priv->go_next = NULL;
+
+  priv->player = NULL;
+  priv->title = NULL;
+  priv->filename = NULL;
+}
+
+static void
+mpris_menu_item_finalize (GObject *object)
+{
+  MprisMenuItem        *self;
+  MprisMenuItemPrivate *priv;
+
+  self = MPRIS_MENU_ITEM (object);
+  priv = GET_PRIVATE (self);
+
+  if (priv->player)
+    g_free (priv->player);
+  if (priv->title)
+    g_free (priv->title);
+  if (priv->filename)
+    g_free (priv->filename);
+
+  g_object_unref (priv->title_label);
+  g_object_unref (priv->artist_label);
+  g_object_unref (priv->button_box);
+  g_object_unref (priv->vbox);
+  g_object_unref (priv->hbox);
+  g_object_unref (priv->go_previous);
+  g_object_unref (priv->play_pause);
+  g_object_unref (priv->go_next);
+  g_object_unref (priv->image);
+
+  (*G_OBJECT_CLASS (mpris_menu_item_parent_class)->finalize) (object);
 }
 
 static GtkWidget *
@@ -631,7 +676,6 @@ mpris_menu_item_new_with_player (const gchar *player,
 {
   MprisMenuItem        *menu_item;
   MprisMenuItemPrivate *priv;
-  GtkWidget            *img;
 
   TRACE("entering");
 
@@ -646,19 +690,11 @@ mpris_menu_item_new_with_player (const gchar *player,
     priv->title = g_strdup(player);
   priv->filename = g_strdup(filename);
 
-  priv->vbox = NULL;
-  priv->hbox = NULL;
-  priv->button_box = NULL;
-
   update_packing (menu_item);
 
   gtk_widget_add_events (GTK_WIDGET(menu_item), GDK_SCROLL_MASK|GDK_POINTER_MOTION_MASK|GDK_BUTTON_MOTION_MASK);
 
-  img = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
-G_GNUC_BEGIN_IGNORE_DEPRECATIONS
-  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), img);
-G_GNUC_END_IGNORE_DEPRECATIONS
-  gtk_image_set_pixel_size (GTK_IMAGE (img), 24);
+  gtk_image_set_from_icon_name (GTK_IMAGE (priv->image), icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
 
   return GTK_WIDGET(menu_item);
 }
diff --git a/panel-plugin/scalemenuitem.c b/panel-plugin/scalemenuitem.c
index a572ecb..db96a09 100644
--- a/panel-plugin/scalemenuitem.c
+++ b/panel-plugin/scalemenuitem.c
@@ -200,8 +200,11 @@ scale_menu_item_finalize (GObject *object)
   self = SCALE_MENU_ITEM (object);
   priv = GET_PRIVATE (self);
 
-  g_free (priv->icon_name);
-  priv->icon_name = NULL;
+  if (priv->icon_name)
+    g_free (priv->icon_name);
+
+  g_object_unref (priv->image);
+  g_object_unref (priv->scale);
 
   (*G_OBJECT_CLASS (scale_menu_item_parent_class)->finalize) (object);
 }
@@ -363,6 +366,10 @@ G_GNUC_END_IGNORE_DEPRECATIONS
   g_signal_connect (priv->scale, "value-changed", G_CALLBACK (scale_menu_item_scale_value_changed), scale_item);
   gtk_widget_add_events (GTK_WIDGET(scale_item), GDK_SCROLL_MASK|GDK_POINTER_MOTION_MASK|GDK_BUTTON_MOTION_MASK);
 
+  /* Keep references to the widgets */
+  g_object_ref (priv->image);
+  g_object_ref (priv->scale);
+
   return GTK_WIDGET(scale_item);
 }
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list