Change to use GPtrArray instead GArray
authorPadraig O'Briain <padraigo@src.gnome.org>
Tue, 1 May 2001 16:44:24 +0000 (16:44 +0000)
committerPadraig O'Briain <padraigo@src.gnome.org>
Tue, 1 May 2001 16:44:24 +0000 (16:44 +0000)
Addess memory mangement issues.

atk/atkrelation.c
atk/atkrelation.h
atk/atkrelationset.c
atk/atkrelationset.h

index e57bf79..07419db 100755 (executable)
@@ -71,15 +71,19 @@ atk_relation_new (AtkObject       **targets,
 {
   AtkRelation *relation;
   int         i;
-  GArray      *array;
+  GPtrArray      *array;
 
   g_return_val_if_fail (targets != NULL, NULL);
 
   relation = g_object_new (ATK_TYPE_RELATION, NULL);
-  array = g_array_sized_new (FALSE, FALSE, sizeof (AtkObject *), n_targets);
+  array = g_ptr_array_sized_new (n_targets);
   for (i = 0; i < n_targets; i++)
   {
-    g_array_insert_vals (array, i, &targets[i], sizeof (AtkObject *));
+    /*
+     * Add a reference to AtkObject being added to a relation
+     */
+    g_object_ref (targets[i]);
+    g_ptr_array_add (array, targets[i]);
   }
   
   relation->target = array;
@@ -97,7 +101,7 @@ atk_relation_get_relation_type (AtkRelation *relation)
   return relation->relationship;
 }
 
-GArray*
+GPtrArray*
 atk_relation_get_target (AtkRelation *relation)
 {
   g_return_val_if_fail (relation != NULL, FALSE);
@@ -117,6 +121,15 @@ atk_relation_finalize (GObject *object)
 
   if (relation->target)
   {
-    g_array_free (relation->target, TRUE);
+    gint i;
+
+    for (i = 0; i < relation->target->len; i++)
+    {
+      /*
+       * Remove a reference to AtkObject being removed from a relation
+       */
+      g_object_unref (g_ptr_array_index (relation->target, i));
+    }
+    g_ptr_array_free (relation->target, TRUE);
   } 
 }
index ea94817..ef24c44 100755 (executable)
@@ -53,7 +53,7 @@ struct _AtkRelation
 {
   GObject parent;
 
-  GArray          *target;
+  GPtrArray       *target;
   AtkRelationType relationship;
 };
 
@@ -80,7 +80,7 @@ AtkRelationType atk_relation_get_relation_type        (AtkRelation     *relation
 /*
  * Returns the target list of a relation.
  */
-GArray*         atk_relation_get_target               (AtkRelation     *relation);
+GPtrArray*      atk_relation_get_target               (AtkRelation     *relation);
 
 #ifdef __cplusplus
 }
index ee4dd54..8897fe2 100755 (executable)
@@ -69,7 +69,7 @@ gboolean
 atk_relation_set_contains (AtkRelationSet   *set,
                            AtkRelationType  relationship)
 {
-  GArray *array_item;
+  GPtrArray *array_item;
   AtkRelation *item;
   gint  i;
 
@@ -81,7 +81,7 @@ atk_relation_set_contains (AtkRelationSet   *set,
     return FALSE;
   for (i = 0; i < array_item->len; i++)
   {
-    item = g_array_index (array_item, AtkRelation*, i);
+    item = g_ptr_array_index (array_item, i);
     if (item->relationship == relationship)
       return TRUE;
   }
@@ -92,9 +92,7 @@ void
 atk_relation_set_remove (AtkRelationSet *set,
                          AtkRelation    *relation)
 {
-  GArray *array_item;
-  AtkRelation *item;
-  gint  i;
+  GPtrArray *array_item;
 
   g_return_if_fail (set != NULL);
   g_return_if_fail (ATK_IS_RELATION_SET (set));
@@ -103,14 +101,10 @@ atk_relation_set_remove (AtkRelationSet *set,
   array_item = set->relations;
   if (array_item == NULL)
     return;
-  for (i = 0; i < array_item->len; i++)
+  
+  if (g_ptr_array_remove (array_item, relation))
   {
-    item = g_array_index (array_item, AtkRelation*, i);
-    if (item == relation)
-    {
-      g_array_remove_index (array_item, i);
-      return;
-    }
+    g_object_unref (relation);
   }
 }
 
@@ -124,9 +118,10 @@ atk_relation_set_add (AtkRelationSet *set,
 
   if (set->relations == NULL)
   {
-    set->relations = g_array_new (FALSE, TRUE, sizeof (AtkRelation));
+    set->relations = g_ptr_array_new ();
   }
-  set->relations = g_array_append_val (set->relations, relation);
+  g_ptr_array_add (set->relations, relation);
+  g_object_ref (relation);
 }
 
 gint
@@ -145,7 +140,7 @@ AtkRelation*
 atk_relation_set_get_relation (AtkRelationSet *set,
                                gint           i)
 {
-  GArray *array_item;
+  GPtrArray *array_item;
   AtkRelation* item;
 
   g_return_val_if_fail (set != NULL, NULL);
@@ -155,7 +150,7 @@ atk_relation_set_get_relation (AtkRelationSet *set,
   array_item = set->relations;
   if (array_item == NULL)
     return NULL;
-  item = g_array_index (array_item, AtkRelation*, i);
+  item = g_ptr_array_index (array_item, i);
   if (item == NULL)
     return NULL;
 
@@ -166,7 +161,7 @@ AtkRelation*
 atk_relation_set_get_relation_by_type (AtkRelationSet  *set,
                                        AtkRelationType relationship)
 {
-  GArray *array_item;
+  GPtrArray *array_item;
   AtkRelation *item;
   gint i;
 
@@ -178,7 +173,7 @@ atk_relation_set_get_relation_by_type (AtkRelationSet  *set,
     return NULL;
   for (i = 0; i < array_item->len; i++)
   {
-    item = g_array_index (array_item, AtkRelation*, i);
+    item = g_ptr_array_index (array_item, i);
     if (item->relationship == relationship)
       return item;
   }
@@ -189,7 +184,7 @@ static void
 atk_relation_set_finalize (GObject *object)
 {
   AtkRelationSet     *relation_set;
-  GArray             *array;
+  GPtrArray             *array;
   gint               i;
 
   g_return_if_fail (ATK_IS_RELATION_SET (object));
@@ -201,8 +196,8 @@ atk_relation_set_finalize (GObject *object)
   {
     for (i = 0; i < array->len; i++)
     {
-      g_object_unref (g_array_index (array, AtkRelation *, i));
+      g_object_unref (g_ptr_array_index (array, i));
     }
-    g_array_free (array, TRUE);
+    g_ptr_array_free (array, TRUE);
   }
 }
index 654598f..2ae685d 100755 (executable)
@@ -42,7 +42,7 @@ struct _AtkRelationSet
 {
   GObject parent;
 
-  GArray *relations;
+  GPtrArray *relations;
 };
 
 struct _AtkRelationSetClass
@@ -52,8 +52,6 @@ struct _AtkRelationSetClass
 
 GType atk_relation_set_get_type (void);
 
-AtkRelationType atk_relation_type_register            (const gchar *name);
-
 /*
  * Create a new relation set.
  */
@@ -65,13 +63,20 @@ AtkRelationSet*    atk_relation_set_new               (void);
 gboolean        atk_relation_set_contains             (AtkRelationSet  *set,
                                                        AtkRelationType relationship);
 /*
- * Remove a relation from the from the relation set.
+ * Remove a relation from the relation set.
+ *
+ * This function unref's the AtkRelation so it will be deleted unless there
+ * is another reference to it. 
  */
 void            atk_relation_set_remove               (AtkRelationSet  *set,
                                                        AtkRelation     *relation);
 /*
  * Add a new relation to the current relation set if it is not already
  * present.
+ *
+ * This function ref's the AtkRelation so the caller of this function 
+ * should unref it to ensure that it will be destroyed when the AtkRelationSet
+ * is destroyed.
  */
 void            atk_relation_set_add                  (AtkRelationSet  *set,
                                                        AtkRelation     *relation);