GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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, GObjectClass *klass)
 
void gnc_gobject_tracking_forget (GObject *object)
 
const GList * gnc_gobject_tracking_get_list (const gchar *name)
 
void gnc_gobject_tracking_dump (void)
 

Detailed Description

Function Documentation

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 102 of file gnc-gobject-utils.c.

103 {
104  GHashTable *table;
105 
106  //printf("Enter %s:\n", G_STRFUNC);
107  table = gnc_gobject_tracking_table();
108 
109  if (g_hash_table_size(table) > 0)
110  {
111  g_message("The following objects remain alive:");
112  g_hash_table_foreach_remove(table, (GHRFunc)gnc_gobject_dump_list, NULL);
113  }
114  //printf("Leave %s:\n", G_STRFUNC);
115 }
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 remember this object in the database.

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

202 {
203  if (gnc_gobject_tracking_forget_internal(object))
204  g_object_weak_unref(object, gnc_gobject_weak_cb, NULL);
205 }
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.

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

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

228 {
229  GHashTable *table;
230  GList *list;
231 
232  //printf("Enter %s: name %s\n", G_STRFUNC, name);
233  table = gnc_gobject_tracking_table();
234  list = g_hash_table_lookup(table, name);
235  //printf("Leave %s: list %p\n", G_STRFUNC, list);
236  return list;
237 }
void gnc_gobject_tracking_remember ( GObject *  object,
GObjectClass *  klass 
)

Tell gnucash to remember this object in the database.

Parameters
objectThe object to be tracked. This can be a fully or partially instantiated object.
klassThe class structure for the object. This argument may be NULL if a fully instantiated object is passed in as the first argument. If a partially instantiated object is provided (I.E. a parent class called this function) then this argument is required. This is necessary because the class of the object changes as each of the parent class is instantiated. The class structure, however, status constant and always reflects the fully instantiated object.

Tell gnucash to remember this object in the database.

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

122 {
123  GHashTable *table;
124  GList *list;
125  const gchar *name;
126 
127  g_return_if_fail(G_IS_OBJECT(object));
128 
129  /* Little dance here to handle startup conditions. During object
130  * initialization the object type changes as each each parent class
131  * is initialized. The class passed to the initialization function
132  * is always the ultimate class of the object. */
133  if (klass == NULL)
134  klass = G_OBJECT_GET_CLASS(object);
135  name = g_type_name(G_TYPE_FROM_CLASS(klass));
136 
137  //printf("Enter %s: object %p of type %s\n", G_STRFUNC, object, name);
138  table = gnc_gobject_tracking_table();
139  list = g_hash_table_lookup(table, name);
140 
141  if (g_list_index(list, object) != -1)
142  {
143  g_critical("Object %p is already in list of %s", object, name);
144  //printf("Leave %s: already in list\n", G_STRFUNC);
145  return;
146  }
147 
148  list = g_list_append(list, object);
149  g_hash_table_insert(table, g_strdup(name), list);
150 
151  g_object_weak_ref(object, gnc_gobject_weak_cb, NULL);
152  //printf("Leave %s:\n", G_STRFUNC);
153 }