[Gnucash-changes] Enhance object tracking to catch the deletion of an object that hasn't

David Hampton hampton at cvs.gnucash.org
Tue Aug 2 22:54:25 EDT 2005


Log Message:
-----------
Enhance object tracking to catch the deletion of an object that hasn't
been removed from the tracking list.

Tags:
----
gnucash-gnome2-dev

Modified Files:
--------------
    gnucash/src/core-utils:
        gnc-gobject-utils.c

Revision Data
-------------
Index: gnc-gobject-utils.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/core-utils/Attic/gnc-gobject-utils.c,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -Lsrc/core-utils/gnc-gobject-utils.c -Lsrc/core-utils/gnc-gobject-utils.c -u -r1.1.2.1 -r1.1.2.2
--- src/core-utils/gnc-gobject-utils.c
+++ src/core-utils/gnc-gobject-utils.c
@@ -30,6 +30,8 @@
 			// a g_source attached to the main glib context.
 
 
+static void gnc_gobject_weak_cb (gpointer user_data, GObject *object);
+
 /************************************************************/
 /*                      Gconf Utilities                     */
 /************************************************************/
@@ -145,20 +147,20 @@
 
   list = g_list_append(list, object);
   g_hash_table_insert(table, g_strdup(name), list);
+
+  g_object_weak_ref(object, gnc_gobject_weak_cb, NULL);
   //printf("Leave %s:\n", __FUNCTION__);
 }
 
 
-/** Tell gnucash to remember this object in the database.
- */
-void
-gnc_gobject_tracking_forget (GObject *object)
+static gboolean
+gnc_gobject_tracking_forget_internal (GObject *object)
 {
   GHashTable *table;
-  GList *list;
+  GList *list, *item;
   const gchar *name;
 
-  g_return_if_fail(G_IS_OBJECT(object));
+  g_return_val_if_fail(G_IS_OBJECT(object), FALSE);
 
   name = G_OBJECT_TYPE_NAME(object);
   //printf("Enter %s: object %p of type %s\n", __FUNCTION__, object, name);
@@ -166,9 +168,17 @@
   list = g_hash_table_lookup(table, name);
   if (!list) {
     //printf("Leave %s: list for %s objects not found.\n", __FUNCTION__, name);
-    return;
+    return FALSE;
+  }
+
+  item = g_list_find(list, object);
+  if (!item) {
+    //printf("Leave %s: object %p not in %s object list.\n", __FUNCTION__,
+    //       object, name);
+    return FALSE;
   }
-  list = g_list_remove(list, object);
+
+  list = g_list_remove_link(list, item);
   if (list) {
     g_hash_table_replace(table, g_strdup(name), list);
     //printf("Leave %s: object removed.\n", __FUNCTION__);
@@ -176,6 +186,33 @@
     g_hash_table_remove(table, name);
     //printf("Leave %s: object and list removed.\n", __FUNCTION__);
   }
+  return TRUE;
+}
+
+
+/** Tell gnucash to remember this object in the database.
+ */
+void
+gnc_gobject_tracking_forget (GObject *object)
+{
+  if (gnc_gobject_tracking_forget_internal(object))
+    g_object_weak_unref(object, gnc_gobject_weak_cb, NULL);
+}
+
+
+/** This callback function is called when a remembered object is
+ *  destroyed.  It is used to catch the destruction an object when the
+ *  caller didn't first tell this code to forget that object.  No need
+ *  for an error message, just forget the object.
+ *
+ *  @internal
+ *
+ *  @param object The object being destroyed.
+ */
+static void
+gnc_gobject_weak_cb (gpointer user_data, GObject *object)
+{
+  gnc_gobject_tracking_forget_internal(object);
 }
 
 


More information about the gnucash-changes mailing list