GnuCash  5.6-150-g038405b370+
Files
Gobject

Files

file  gnc-gobject-utils.h
 Gobject helper routines.
 

Gobject Tracking Functions

This set of functions is used to maintain a "database" of objects that are built on top of a GObject (any level of nesting).

This database is simply a hash table of lists. The hash table takes the object name as its key and returns a list of all objects of that type. The object is then added to, deleted from, or looked up in the list. The database can also be queried for a list of all objects of a specified type. This can be used to find pre-existing GncTreeModels, etc. (In this case performing a search for a specific object wouldn't help because the information being inspected is private to the object.)

Any object added to this database during the execution of gnucash should be deleted from it before completion of the program. WHen the program shuts down, a list of all objects still in the database will be dumped out to the logfile. This should help developers find memory leaks in their code where an object is lost, or is not release because it gained an extra reference at some point during its lifetime.

void gnc_gobject_tracking_remember (GObject *object)
 Tell gnucash to remember this object in the database. More...
 
void gnc_gobject_tracking_forget (GObject *object)
 Tell gnucash to drop this object from the database. More...
 
const GList * gnc_gobject_tracking_get_list (const gchar *name)
 Get a list of all known objects of a specified type. More...
 
void gnc_gobject_tracking_dump (void)
 Dump the entire object tracking database via the g_log() family of functions. More...
 

Detailed Description

Function Documentation

◆ gnc_gobject_tracking_dump()

void gnc_gobject_tracking_dump ( void  )

Dump the entire object tracking database via the g_log() family of functions.

This function is only called when gnucash exits, and at that point all of the objects should have been removed from the database and freed. Any object remaining is the result of a memory/object leakage.

Definition at line 104 of file gnc-gobject-utils.c.

105 {
106  GHashTable *table;
107 
108  //printf("Enter %s:\n", G_STRFUNC);
109  table = gnc_gobject_tracking_table();
110 
111  if (g_hash_table_size(table) > 0)
112  {
113  PINFO("The following objects remain alive:");
114  g_hash_table_foreach_remove(table, (GHRFunc)gnc_gobject_dump_list, NULL);
115  }
116  //printf("Leave %s:\n", G_STRFUNC);
117 }
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256

◆ gnc_gobject_tracking_forget()

void gnc_gobject_tracking_forget ( GObject *  object)

Tell gnucash to drop this object from the database.

Parameters
objectThe object to be dropped.

Tell gnucash to drop this object from the database.

Definition at line 194 of file gnc-gobject-utils.c.

195 {
196  if (gnc_gobject_tracking_forget_internal(object))
197  g_object_weak_unref(object, gnc_gobject_weak_cb, NULL);
198 }

◆ gnc_gobject_tracking_get_list()

const GList* gnc_gobject_tracking_get_list ( const gchar *  name)

Get a list of all known objects of a specified type.

Parameters
nameThe type name of the objects to be found. This is the name used when the object type was initialized. If unknown, it can be found by calling G_OBJECT_TYPE_NAME(object).
Returns
A GList of objects of the specified type. This list is owned by the tracking code and must not be modified by the caller.

Definition at line 220 of file gnc-gobject-utils.c.

221 {
222  GHashTable *table;
223  GList *list;
224 
225  //printf("Enter %s: name %s\n", G_STRFUNC, name);
226  table = gnc_gobject_tracking_table();
227  list = g_hash_table_lookup(table, name);
228  //printf("Leave %s: list %p\n", G_STRFUNC, list);
229  return list;
230 }

◆ gnc_gobject_tracking_remember()

void gnc_gobject_tracking_remember ( GObject *  object)

Tell gnucash to remember this object in the database.

Parameters
objectThe fully constructed object to be tracked.

Definition at line 123 of file gnc-gobject-utils.c.

124 {
125  g_return_if_fail(G_IS_OBJECT(object));
126 
127  GObjectClass *klass = G_OBJECT_GET_CLASS(object);
128  const gchar *name = g_type_name(G_TYPE_FROM_CLASS(klass));
129 
130  //printf("Enter %s: object %p of type %s\n", G_STRFUNC, object, name);
131  GHashTable *table = gnc_gobject_tracking_table();
132  GList *list = g_hash_table_lookup(table, name);
133 
134  if (g_list_index(list, object) != -1)
135  {
136  g_critical("Object %p is already in list of %s", object, name);
137  //printf("Leave %s: already in list\n", G_STRFUNC);
138  return;
139  }
140 
141  list = g_list_append(list, object);
142  g_hash_table_insert(table, g_strdup(name), list);
143 
144  g_object_weak_ref(object, gnc_gobject_weak_cb, NULL);
145  //printf("Leave %s:\n", G_STRFUNC);
146 }