d8c875
From 7cf6268e54620bbbe5e6e61800c50fb0cb4bea57 Mon Sep 17 00:00:00 2001
d8c875
From: =?UTF-8?q?Corentin=20No=C3=ABl?= <corentin@elementary.io>
d8c875
Date: Fri, 16 Oct 2020 19:56:26 +0200
d8c875
Subject: [PATCH] Change GLib.PtrArray into GLib.GenericArray
d8c875
d8c875
This is the vala-friendly way of handling GPtrArray.
d8c875
Fix several memory leaks on the go and unnecessary reference increase.
d8c875
---
d8c875
 src/cheese-preferences.vala | 26 ++++++++++++--------------
d8c875
 src/cheese-window.vala      | 22 +++++++++++-----------
d8c875
 src/vapi/cheese-common.vapi |  2 +-
d8c875
 3 files changed, 24 insertions(+), 26 deletions(-)
d8c875
d8c875
diff --git a/src/cheese-preferences.vala b/src/cheese-preferences.vala
d8c875
index f56af7e0..80a92431 100644
d8c875
--- a/src/cheese-preferences.vala
d8c875
+++ b/src/cheese-preferences.vala
d8c875
@@ -100,7 +100,7 @@ public PreferencesDialog (Cheese.Camera camera)
d8c875
    */
d8c875
   private void initialize_camera_devices ()
d8c875
   {
d8c875
-    unowned GLib.PtrArray devices = camera.get_camera_devices ();
d8c875
+    GLib.GenericArray<unowned Cheese.CameraDevice> devices = camera.get_camera_devices ();
d8c875
     camera_model = new Gtk.ListStore (2, typeof (string), typeof (Cheese.CameraDevice));
d8c875
 
d8c875
     source_combo.model = camera_model;
d8c875
@@ -357,13 +357,13 @@ public PreferencesDialog (Cheese.Camera camera)
d8c875
    */
d8c875
   private void on_camera_update_num_camera_devices ()
d8c875
   {
d8c875
-    unowned GLib.PtrArray devices = camera.get_camera_devices ();
d8c875
-    Cheese.CameraDevice   dev;
d8c875
+    GLib.GenericArray<unowned Cheese.CameraDevice> devices = camera.get_camera_devices ();
d8c875
+    unowned Cheese.CameraDevice   dev;
d8c875
 
d8c875
     // Add (if) / Remove (else) a camera device.
d8c875
-    if (devices.len > camera_model.iter_n_children (null))
d8c875
+    if (devices.length > camera_model.iter_n_children (null))
d8c875
     {
d8c875
-      dev = (Cheese.CameraDevice) devices.index (devices.len - 1);
d8c875
+      dev = devices.get (devices.length - 1);
d8c875
       add_camera_device(dev);
d8c875
     }
d8c875
     else
d8c875
@@ -382,12 +382,11 @@ public PreferencesDialog (Cheese.Camera camera)
d8c875
       bool device_removed = false;
d8c875
       devices.foreach ((device) =>
d8c875
       {
d8c875
-        var old_device = (Cheese.CameraDevice) device;
d8c875
         Cheese.CameraDevice new_device;
d8c875
         camera_model.get (iter, 1, out new_device, -1);
d8c875
 
d8c875
         // Found the device that was removed.
d8c875
-        if (old_device != new_device)
d8c875
+        if (device != new_device)
d8c875
         {
d8c875
             remove_camera_device (iter, new_device, active_device);
d8c875
             device_removed = true;
d8c875
@@ -418,17 +417,16 @@ public PreferencesDialog (Cheese.Camera camera)
d8c875
    *
d8c875
    * @param device a Cheese.CameraDevice to add to the device combo box model
d8c875
    */
d8c875
-  private void add_camera_device (void *device)
d8c875
+  private void add_camera_device (Cheese.CameraDevice device)
d8c875
   {
d8c875
     TreeIter iter;
d8c875
-    Cheese.CameraDevice dev = (Cheese.CameraDevice) device;
d8c875
 
d8c875
     camera_model.append (out iter);
d8c875
     camera_model.set (iter,
d8c875
-                      0, dev.get_name (),
d8c875
-                      1, dev);
d8c875
+                      0, device.get_name (),
d8c875
+                      1, device);
d8c875
 
d8c875
-    if (camera.get_selected_device () == dev)
d8c875
+    if (camera.get_selected_device () == device)
d8c875
         source_combo.set_active_iter (iter);
d8c875
 
d8c875
     if (camera_model.iter_n_children (null) > 1)
d8c875
@@ -445,12 +443,12 @@ public PreferencesDialog (Cheese.Camera camera)
d8c875
   private void remove_camera_device (TreeIter iter, Cheese.CameraDevice device_node,
d8c875
                              Cheese.CameraDevice active_device_node)
d8c875
   {
d8c875
-      unowned GLib.PtrArray devices = camera.get_camera_devices ();
d8c875
+      GLib.GenericArray<unowned Cheese.CameraDevice> devices = camera.get_camera_devices ();
d8c875
 
d8c875
       // Check if the camera that we want to remove, is the active one
d8c875
       if (device_node == active_device_node)
d8c875
       {
d8c875
-        if (devices.len > 0)
d8c875
+        if (devices.length > 0)
d8c875
           set_new_available_camera_device (iter);
d8c875
         else
d8c875
           this.hide ();
d8c875
diff --git a/src/cheese-window.vala b/src/cheese-window.vala
d8c875
index ff069808..cc119b68 100644
d8c875
--- a/src/cheese-window.vala
d8c875
+++ b/src/cheese-window.vala
d8c875
@@ -1216,9 +1216,9 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
d8c875
    */
d8c875
   public void on_switch_camera_clicked ()
d8c875
   {
d8c875
-      Cheese.CameraDevice selected;
d8c875
-      Cheese.CameraDevice next = null;
d8c875
-      GLib.PtrArray cameras;
d8c875
+      unowned Cheese.CameraDevice selected;
d8c875
+      unowned Cheese.CameraDevice next = null;
d8c875
+      GLib.GenericArray<unowned Cheese.CameraDevice> cameras;
d8c875
       uint i;
d8c875
 
d8c875
       if (camera == null)
d8c875
@@ -1235,9 +1235,9 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
d8c875
 
d8c875
       cameras = camera.get_camera_devices ();
d8c875
 
d8c875
-      for (i = 0; i < cameras.len; i++)
d8c875
+      for (i = 0; i < cameras.length; i++)
d8c875
       {
d8c875
-          next = (Cheese.CameraDevice )cameras.index (i);
d8c875
+          next = cameras.get (i);
d8c875
 
d8c875
           if (next == selected)
d8c875
           {
d8c875
@@ -1245,13 +1245,13 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
d8c875
           }
d8c875
       }
d8c875
 
d8c875
-      if (i + 1 < cameras.len)
d8c875
+      if (i + 1 < cameras.length)
d8c875
       {
d8c875
-          next = (Cheese.CameraDevice )cameras.index (i + 1);
d8c875
+          next = cameras.get (i + 1);
d8c875
       }
d8c875
       else
d8c875
       {
d8c875
-          next = (Cheese.CameraDevice )cameras.index (0);
d8c875
+          next = cameras.get (0);
d8c875
       }
d8c875
 
d8c875
       if (next == selected)
d8c875
@@ -1269,8 +1269,8 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
d8c875
    */
d8c875
   public void set_switch_camera_button_state ()
d8c875
   {
d8c875
-      Cheese.CameraDevice selected;
d8c875
-      GLib.PtrArray cameras;
d8c875
+      unowned Cheese.CameraDevice selected;
d8c875
+      GLib.GenericArray<unowned Cheese.CameraDevice> cameras;
d8c875
 
d8c875
       if (camera == null)
d8c875
       {
d8c875
@@ -1288,7 +1288,7 @@ public class Cheese.MainWindow : Gtk.ApplicationWindow
d8c875
 
d8c875
       cameras = camera.get_camera_devices ();
d8c875
 
d8c875
-      if (cameras.len > 1)
d8c875
+      if (cameras.length > 1)
d8c875
       {
d8c875
          switch_camera_button.set_visible (true);
d8c875
          return;
d8c875
diff --git a/src/vapi/cheese-common.vapi b/src/vapi/cheese-common.vapi
d8c875
index 6517cdfc..e4ae7ad3 100644
d8c875
--- a/src/vapi/cheese-common.vapi
d8c875
+++ b/src/vapi/cheese-common.vapi
d8c875
@@ -35,7 +35,7 @@ namespace Cheese
d8c875
     [CCode (has_construct_function = false)]
d8c875
     public Camera (Clutter.Actor video_texture, string camera_device_node, int x_resolution, int y_resolution);
d8c875
     public bool                        get_balance_property_range (string property, double min, double max, double def);
d8c875
-    public unowned GLib.PtrArray       get_camera_devices ();
d8c875
+    public GLib.GenericArray<unowned Cheese.CameraDevice> get_camera_devices ();
d8c875
     public unowned Cheese.VideoFormat  get_current_video_format ();
d8c875
     public int                         get_num_camera_devices ();
d8c875
     public unowned Cheese.CameraDevice get_selected_device ();
d8c875
-- 
d8c875
GitLab
d8c875