Fix huge memory leak

Implement proper handling of imgstore reference counting and remove
several leaky memdup functions
This commit is contained in:
mjentsch 2014-12-27 22:43:31 +01:00
parent e3caa6acdb
commit 00ece8a3b4
3 changed files with 25 additions and 4 deletions

View file

@ -235,13 +235,16 @@ struct tgl_update_callback tgp_callback = {
};
void on_message_load_photo (struct tgl_state *TLS, void *extra, int success, char *filename) {
connection_data *conn = TLS->ev_base;
gchar *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
int imgStoreId = purple_imgstore_add_with_id (g_memdup(data, (guint)len), len, NULL);
char *image = format_img_full(imgStoreId);
used_images_add (conn, imgStoreId);
char *image = format_img_full (imgStoreId);
struct tgl_message *M = extra;
switch (tgl_get_peer_type (M->to_id)) {
case TGL_PEER_CHAT:
@ -268,7 +271,7 @@ void on_message_load_photo (struct tgl_state *TLS, void *extra, int success, cha
}
g_free (image);
connection_data *conn = TLS->ev_base;
conn = TLS->ev_base;
conn->updated = 1;
}
@ -443,7 +446,9 @@ static void on_userpic_loaded (struct tgl_state *TLS, void *extra, int success,
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
int imgStoreId = purple_imgstore_add_with_id (g_memdup(data, (guint)len), len, NULL);
used_images_add (conn, imgStoreId);
struct download_desc *dld = extra;
struct tgl_user *U = dld->data;
@ -460,7 +465,7 @@ static void on_userpic_loaded (struct tgl_state *TLS, void *extra, int success,
purple_notify_userinfo (conn->gc, who, info, NULL, NULL);
g_free (profile_image);
}
purple_buddy_icons_set_for_user(conn->pa, who, g_memdup(data, (guint)len), len, NULL);
purple_buddy_icons_set_for_user(conn->pa, who, data, len, NULL);
g_free(who);
}

View file

@ -83,6 +83,18 @@ void message_text_free (gpointer data)
free (mt);
}
static void used_image_free (gpointer data)
{
int id = GPOINTER_TO_INT(data);
purple_imgstore_unref_by_id (id);
debug ("used_image: unref %d", id);
}
void used_images_add (connection_data *data, gint imgid)
{
data->used_images = g_list_append (data->used_images, GINT_TO_POINTER(imgid));
debug ("used_image: add %d", imgid);
}
connection_data *connection_data_init (struct tgl_state *TLS, PurpleConnection *gc, PurpleAccount *pa)
{
@ -102,6 +114,7 @@ void *connection_data_free (connection_data *conn)
g_queue_free_full (conn->pending_reads, pending_reads_free_cb);
g_queue_free_full (conn->new_messages, message_text_free);
g_hash_table_destroy (conn->joining_chats);
g_list_free_full (conn->used_images, used_image_free);
tgl_free_all (conn->TLS);
free (conn);
return NULL;

View file

@ -34,6 +34,7 @@ typedef struct {
int updated;
GQueue *new_messages;
GQueue *pending_reads;
GList *used_images;
GHashTable *joining_chats;
guint timer;
int in_fallback_chat;
@ -55,6 +56,8 @@ void pending_reads_add (GQueue *queue, tgl_peer_id_t id);
struct message_text *message_text_init (struct tgl_message *M, gchar *text);
void message_text_free (gpointer data);
void used_images_add (connection_data *data, gint imgid);
void *connection_data_free (connection_data *conn);
connection_data *connection_data_init (struct tgl_state *TLS, PurpleConnection *gc, PurpleAccount *pa);