[nautilus-python] Fix memory leaks noticed by Nemo



commit 561d3e4cd4a9f74c8a433e7638b1d2929fe50da1
Author: Jan Tojnar <jtojnar gmail com>
Date:   Fri Aug 12 03:07:55 2022 +0200

    Fix memory leaks noticed by Nemo
    
    Inspired by https://github.com/linuxmint/nemo-extensions/commit/f2a2ccf737ca1ca475d4c60d7bc00b190b83f7e0

 src/nautilus-python-object.c |  4 ++--
 src/nautilus-python.c        | 17 ++++++++---------
 2 files changed, 10 insertions(+), 11 deletions(-)
---
diff --git a/src/nautilus-python-object.c b/src/nautilus-python-object.c
index d6c77f3..da84bc3 100644
--- a/src/nautilus-python-object.c
+++ b/src/nautilus-python-object.c
@@ -448,8 +448,8 @@ nautilus_python_object_class_init (NautilusPythonObjectClass *class,
 GType 
 nautilus_python_object_get_type (GTypeModule *module, 
                                  PyObject     *type) {
-    GTypeInfo *info;
-    const char *type_name;
+    g_autofree GTypeInfo *info = NULL;
+    g_autofree gchar *type_name = NULL;
     GType gtype;
       
     static const GInterfaceInfo property_page_provider_interface_info = {
diff --git a/src/nautilus-python.c b/src/nautilus-python.c
index fafcaf9..5f61797 100644
--- a/src/nautilus-python.c
+++ b/src/nautilus-python.c
@@ -122,12 +122,8 @@ nautilus_python_load_dir (GTypeModule *module,
             
     while ((name = g_dir_read_name(dir))) {
         if (g_str_has_suffix(name, ".py")) {
-            char *modulename;
-            int len;
-
-            len = strlen(name) - 3;
-            modulename = g_new0(char, len + 1 );
-            strncpy(modulename, name, len);
+            size_t len = strlen (name) - 3;
+            g_autofree char *modulename = g_strndup (name, len);
 
             if (!initialized) {
                 PyObject *sys_path, *py_path;
@@ -136,7 +132,6 @@ nautilus_python_load_dir (GTypeModule *module,
                 * at all if no extensions are found) */
                 if (!nautilus_python_init_python()) {
                     g_warning("nautilus_python_init_python failed");
-                    g_dir_close(dir);
                     break;
                 }
                 
@@ -150,6 +145,8 @@ nautilus_python_load_dir (GTypeModule *module,
             nautilus_python_load_file(module, modulename);
         }
     }    
+
+    g_dir_close (dir);
 }
 
 static gboolean
@@ -242,7 +239,7 @@ nautilus_python_check_all_directories(GTypeModule *module) {
     // If nautilus is built in a non-standard prefix
     // Check nautilus prefix's DATADIR
     gchar *prefix_extension_dir = DATADIR "/nautilus-python/extensions";
-    dirs = g_list_append(dirs, prefix_extension_dir);
+    dirs = g_list_append(dirs, g_strdup (prefix_extension_dir));
 
     // Check all system data dirs 
     const gchar *const *temp = g_get_system_data_dirs();
@@ -251,6 +248,8 @@ nautilus_python_check_all_directories(GTypeModule *module) {
             "nautilus-python", "extensions", NULL);
         if (g_strcmp0(dir, prefix_extension_dir) != 0) {
             dirs = g_list_append(dirs, dir);
+        } else {
+            g_free (dir);
         }
 
         temp++;
@@ -263,7 +262,7 @@ nautilus_python_check_all_directories(GTypeModule *module) {
         dirs = dirs->next;
     }
 
-    g_list_free(dirs);
+    g_list_free_full (dirs, g_free);
 }
 
 void


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