]> git.0d.be Git - empathy.git/commitdiff
empathy_avatar_new: stop stealing strings (#650939)
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Thu, 26 May 2011 09:10:06 +0000 (11:10 +0200)
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>
Wed, 1 Jun 2011 09:58:09 +0000 (11:58 +0200)
It was pretty confusing when reading/reviewing code.

libempathy-gtk/empathy-avatar-chooser.c
libempathy/empathy-contact.c
libempathy/empathy-contact.h

index ff278590849cf920efbe40323dedeb6b2398cabe..2d1c599431e66807455cb3eed5b7f3605ea7221d 100644 (file)
@@ -607,10 +607,12 @@ avatar_chooser_maybe_convert_and_scale (EmpathyAvatarChooser *self,
 
   g_free (new_format_name);
 
-  /* Takes ownership of new_mime_type and best_image_data */
   avatar = empathy_avatar_new ((guchar *) best_image_data,
     best_image_size, new_mime_type, NULL);
 
+  g_free (best_image_data);
+  g_free (new_mime_type);
+
   return avatar;
 }
 
@@ -655,6 +657,7 @@ avatar_chooser_set_image (EmpathyAvatarChooser *self,
   g_object_unref (pixbuf);
 }
 
+/* takes ownership of @data */
 static void
 avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
     gchar *data,
@@ -679,10 +682,12 @@ avatar_chooser_set_image_from_data (EmpathyAvatarChooser *self,
       return;
     }
 
-  /* avatar takes ownership of data and mime_type */
   avatar = empathy_avatar_new ((guchar *) data, size, mime_type, NULL);
 
   avatar_chooser_set_image (self, avatar, pixbuf, set_locally);
+
+  g_free (mime_type);
+  g_free (data);
 }
 
 static void
@@ -730,12 +735,8 @@ avatar_chooser_drag_data_received_cb (GtkWidget          *widget,
 
       if (handled)
         {
-          /* this in turn calls empathy_avatar_new (), which assumes
-           * ownership of data.
-           */
-          avatar_chooser_set_image_from_data (self, data,
-                      bytes_read,
-                      TRUE);
+          /* pass data to the avatar_chooser_set_image_from_data */
+          avatar_chooser_set_image_from_data (self, data, bytes_read, TRUE);
         }
 
       g_object_unref (file);
@@ -801,6 +802,7 @@ avatar_chooser_set_image_from_file (EmpathyAvatarChooser *self,
       return;
     }
 
+  /* pass image_data to the avatar_chooser_set_image_from_data */
   avatar_chooser_set_image_from_data (self, image_data, image_size, TRUE);
 }
 
@@ -809,8 +811,6 @@ static void
 avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
                GdkPixbuf *pb)
 {
-  /* dup the string as empathy_avatar_new steals ownership of the it */
-  gchar *mime = g_strdup ("image/png");
   gsize size;
   gchar *buf;
   EmpathyAvatar *avatar = NULL;
@@ -825,8 +825,10 @@ avatar_chooser_set_avatar_from_pixbuf (EmpathyAvatarChooser *self,
       return;
     }
 
-  avatar = empathy_avatar_new ((guchar *) buf, size, mime, NULL);
+  avatar = empathy_avatar_new ((guchar *) buf, size, "image/png", NULL);
   avatar_chooser_set_image (self, avatar, pb, TRUE);
+
+  g_free (buf);
 }
 
 static gboolean
index f3e200e270712713744c34b81b73bf31fb7068f4..7b23df0fcc38e5f32ed9f91df6fba6822ff160fd 100644 (file)
@@ -1282,17 +1282,16 @@ contact_load_avatar_cache (EmpathyContact *contact,
         }
     }
 
-  if (data)
+  if (data != NULL)
     {
       DEBUG ("Avatar loaded from %s", filename);
       avatar = empathy_avatar_new ((guchar *) data, len, NULL, filename);
       contact_set_avatar (contact, avatar);
       empathy_avatar_unref (avatar);
     }
-  else
-    {
-      g_free (filename);
-    }
+
+  g_free (data);
+  g_free (filename);
 
   return data != NULL;
 }
@@ -1319,24 +1318,23 @@ empathy_avatar_get_type (void)
  * @format: the mime type of the avatar image
  * @filename: the filename where the avatar is stored in cache
  *
- * Create a #EmpathyAvatar from the provided data. This function takes the
- * ownership of @data, @format and @filename.
+ * Create a #EmpathyAvatar from the provided data.
  *
  * Returns: a new #EmpathyAvatar
  */
 EmpathyAvatar *
-empathy_avatar_new (guchar *data,
+empathy_avatar_new (const guchar *data,
                     gsize len,
-                    gchar *format,
-                    gchar *filename)
+                    const gchar *format,
+                    const gchar *filename)
 {
   EmpathyAvatar *avatar;
 
   avatar = g_slice_new0 (EmpathyAvatar);
-  avatar->data = data;
+  avatar->data = g_memdup (data, len);
   avatar->len = len;
-  avatar->format = format;
-  avatar->filename = filename;
+  avatar->format = g_strdup (format);
+  avatar->filename = g_strdup (filename);
   avatar->refcount = 1;
 
   return avatar;
@@ -1783,12 +1781,17 @@ contact_set_avatar_from_tp_contact (EmpathyContact *contact)
       EmpathyAvatar *avatar;
       gchar *data;
       gsize len;
+      gchar *path;
 
       g_file_load_contents (file, NULL, &data, &len, NULL, NULL);
-      avatar = empathy_avatar_new ((guchar *) data, len, g_strdup (mime),
-          g_file_get_path (file));
+      path = g_file_get_path (file);
+
+      avatar = empathy_avatar_new ((guchar *) data, len, mime, path);
+
       contact_set_avatar (contact, avatar);
       empathy_avatar_unref (avatar);
+      g_free (path);
+      g_free (data);
     }
   else
     {
index 16b50e500c4907472cb114b5dcde189e26413129..b32971a3faf41de92c0430ed490549b065c6c37e 100644 (file)
@@ -117,10 +117,10 @@ gboolean empathy_contact_can_do_action (EmpathyContact *self,
 
 #define EMPATHY_TYPE_AVATAR (empathy_avatar_get_type ())
 GType empathy_avatar_get_type (void) G_GNUC_CONST;
-EmpathyAvatar * empathy_avatar_new (guchar *data,
+EmpathyAvatar * empathy_avatar_new (const guchar *data,
     gsize len,
-    gchar *format,
-    gchar *filename);
+    const gchar *format,
+    const gchar *filename);
 EmpathyAvatar * empathy_avatar_ref (EmpathyAvatar *avatar);
 void empathy_avatar_unref (EmpathyAvatar *avatar);