From 9b35b89fce2499d5352f5323baec53abbf9a4af2 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 3 Feb 2019 23:13:40 +0100
Subject: Fixed various memory leaks.

---
 plugins/pychrysalide/pychrysa.c | 35 ++++++++++++++++++++++++++++++++++-
 src/arch/processor.c            |  8 ++++++++
 src/common/array.c              |  7 ++++---
 src/core/global.c               |  2 --
 src/format/format.c             |  5 ++---
 src/gui/editor.c                |  2 ++
 src/main.c                      |  2 ++
 7 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/plugins/pychrysalide/pychrysa.c b/plugins/pychrysalide/pychrysa.c
index 928743d..723c2bc 100644
--- a/plugins/pychrysalide/pychrysa.c
+++ b/plugins/pychrysalide/pychrysa.c
@@ -94,6 +94,9 @@ static bool is_current_abi_suitable(void);
 /* Définit la version attendue de GTK à charger dans Python. */
 static bool set_version_for_gtk_namespace(const char *);
 
+/* Point de sortie pour l'initialisation de Python. */
+static void PyExit_pychrysalide(void);
+
 /* Complète les chemins de recherches de Python. */
 static void extend_python_path(const char *);
 
@@ -292,6 +295,27 @@ static bool set_version_for_gtk_namespace(const char *version)
 *                                                                             *
 *  Paramètres  : -                                                            *
 *                                                                             *
+*  Description : Point de sortie pour l'initialisation de Python.             *
+*                                                                             *
+*  Retour      : ?                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void PyExit_pychrysalide(void)
+{
+    extern void set_current_project(void *project);
+
+    set_current_project(NULL);
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
 *  Description : Point d'entrée pour l'initialisation de Python.              *
 *                                                                             *
 *  Retour      : ?                                                            *
@@ -316,6 +340,7 @@ PyMODINIT_FUNC PyInit_pychrysalide(void)
 {
     PyObject *result;                       /* Module Python à retourner   */
     bool status;                            /* Bilan des inclusions        */
+    int ret;                                /* Bilan de préparatifs        */
     GPluginModule *self;                    /* Représentation interne      */
     PluginStatusFlags self_flags;           /* Fanions à mettre à jour     */
 
@@ -427,12 +452,20 @@ PyMODINIT_FUNC PyInit_pychrysalide(void)
 
     if (!status)
     {
-        PyErr_SetString(PyExc_SystemError, "fail to load all PyChrysalide components.");
+        PyErr_SetString(PyExc_SystemError, "failed to load all PyChrysalide components.");
         return NULL;
     }
 
     if (_standalone)
     {
+        ret = Py_AtExit(PyExit_pychrysalide);
+
+        if (ret == -1)
+        {
+            PyErr_SetString(PyExc_SystemError, "failed to register a cleanup function.");
+            return NULL;
+        }
+
         /**
          * Comme les sources locales sont prioritaires, le fichier "core/global.h"
          * du greffon masque la fonction suivante, issue du corps principal du
diff --git a/src/arch/processor.c b/src/arch/processor.c
index 4d7bacc..05e3206 100644
--- a/src/arch/processor.c
+++ b/src/arch/processor.c
@@ -187,6 +187,11 @@ static void g_arch_processor_init(GArchProcessor *proc)
 
 static void g_arch_processor_dispose(GArchProcessor *proc)
 {
+    size_t i;                               /* Boucle de parcours          */
+
+    for (i = 0; i < proc->instr_count; i++)
+        g_clear_object(&proc->instructions[i]);
+
     g_mutex_clear(&proc->mutex);
 
     g_mutex_clear(&proc->error_mutex);
@@ -212,6 +217,9 @@ static void g_arch_processor_finalize(GArchProcessor *proc)
 {
     size_t i;                               /* Boucle de parcours          */
 
+    if (proc->instructions != NULL)
+        free(proc->instructions);
+
     if (proc->errors != NULL)
     {
         for (i = 0; i < proc->error_count; i++)
diff --git a/src/common/array.c b/src/common/array.c
index 8264101..3f721be 100644
--- a/src/common/array.c
+++ b/src/common/array.c
@@ -285,7 +285,7 @@ void copy_flat_array_items(flat_array_t **src, flat_array_t **dest, size_t size,
     {
         extended = EXTENDED_ARRAY(*src);
 
-        new_ext = (ext_flat_array_t *)malloc(sizeof(ext_flat_array_t));
+        new_ext = malloc(sizeof(ext_flat_array_t));
 
         new_ext->items = malloc(extended->count * size);
         new_ext->count = extended->count;
@@ -387,7 +387,7 @@ void add_item_to_flat_array(flat_array_t **array, const void *item, size_t size)
     {
         if (FLAT_ARRAY_HAS_NO_INDEX(*array))
         {
-            extended = (ext_flat_array_t *)malloc(sizeof(ext_flat_array_t));
+            extended = malloc(sizeof(ext_flat_array_t));
 
             extended->items = malloc(2 * size);
             extended->count = 2;
@@ -456,7 +456,7 @@ void insert_item_into_flat_array(flat_array_t **array, void *item, size_t size,
     {
         if (FLAT_ARRAY_HAS_NO_INDEX(*array))
         {
-            extended = (ext_flat_array_t *)malloc(sizeof(ext_flat_array_t));
+            extended = malloc(sizeof(ext_flat_array_t));
 
             extended->items = malloc(size);
             extended->count = 1;
@@ -581,6 +581,7 @@ void rem_item_from_flat_array(flat_array_t **array, size_t index, size_t size)
             else
                 memcpy(new, ((char *)extended->items) + size, size);
 
+            free(extended->items);
             free(extended);
 
             relock_flat_array(array, new);
diff --git a/src/core/global.c b/src/core/global.c
index 3777fd9..f61def6 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -283,8 +283,6 @@ GStudyProject *get_current_project(void)
 
 void register_project_change_notification(current_project_change_cb notify)
 {
-    assert(_project_notify == NULL);
-
     _project_notify = notify;
 
 }
diff --git a/src/format/format.c b/src/format/format.c
index 261f152..cd71a21 100644
--- a/src/format/format.c
+++ b/src/format/format.c
@@ -156,10 +156,9 @@ static void g_binary_format_init(GBinFormat *format)
 
 static void g_binary_format_dispose(GBinFormat *format)
 {
-    if (format->demangler != NULL)
-        g_object_unref(format->demangler);
+    g_clear_object(&format->demangler);
 
-    g_object_unref(format->info);
+    g_clear_object(&format->info);
 
     g_rw_lock_clear(&format->syms_lock);
 
diff --git a/src/gui/editor.c b/src/gui/editor.c
index 205c78f..58dda53 100644
--- a/src/gui/editor.c
+++ b/src/gui/editor.c
@@ -408,6 +408,8 @@ static void on_destroy_editor(GtkWidget *widget, GObject *ref)
 
     on_focus_out(widget, NULL, ref);
 
+    /* On évite de mettre à jour un affichage disparu... */
+    register_project_change_notification(NULL);
 
     /* Si la boucle principale est bien lancée, on en sort ! */
     if (gtk_main_level() > 0)
diff --git a/src/main.c b/src/main.c
index 34aea01..9ae3936 100644
--- a/src/main.c
+++ b/src/main.c
@@ -388,6 +388,8 @@ int main(int argc, char **argv)
     else
         gtk_main();
 
+    set_current_project(NULL);
+
  bad_project:
 
     if (server != NULL)
-- 
cgit v0.11.2-87-g4458