pidgin/pidgin

Fix leaks in dynamic menus

15 months ago, Elliott Sales de Andrade
33ed4a2a7593
Fix leaks in dynamic menus

Fixes two leaks:
```
10,463 (48 direct, 10,415 indirect) bytes in 1 blocks are definitely lost in loss record 34,138 of 34,479
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4980168: g_malloc (gmem.c:130)
by 0x4997AB5: g_slice_alloc (gslice.c:1074)
by 0x49980EC: g_slice_alloc0 (gslice.c:1100)
by 0x4DDF04B: g_type_create_instance (gtype.c:1913)
by 0x4DC4C1F: g_object_new_internal (gobject.c:2228)
by 0x4DC6247: g_object_new_with_properties (gobject.c:2391)
by 0x4DC6FF0: g_object_new (gobject.c:2037)
by 0x1CA978CF: purple_demo_protocol_get_menu (purpledemoprotocolactions.c:750)
by 0x4D2DB1F: purple_protocol_actions_get_menu (purpleprotocolactions.c:110)
by 0x489F74C: pidgin_accounts_enabled_menu_get_item_links (pidginaccountsenabledmenu.c:279)
by 0x5817C3F: g_menu_model_real_get_item_link (gmenumodel.c:397)
```
and
```
2,051 (144 direct, 1,907 indirect) bytes in 3 blocks are definitely lost in loss record 33,018 of 34,479
at 0x484386F: malloc (vg_replace_malloc.c:393)
by 0x4980168: g_malloc (gmem.c:130)
by 0x4997AB5: g_slice_alloc (gslice.c:1074)
by 0x49980EC: g_slice_alloc0 (gslice.c:1100)
by 0x4DDF04B: g_type_create_instance (gtype.c:1913)
by 0x4DC4C1F: g_object_new_internal (gobject.c:2228)
by 0x4DC6247: g_object_new_with_properties (gobject.c:2391)
by 0x4DC6FF0: g_object_new (gobject.c:2037)
by 0x581E9EE: g_menu_item_new_section (gmenu.c:1256)
by 0x48B465F: pidgin_plugins_menu_get_item_links (pidginpluginsmenu.c:204)
by 0x5817C3F: g_menu_model_real_get_item_link (gmenumodel.c:397)
by 0x5124B78: gtk_menu_tracker_add_items (gtkmenutracker.c:313)
```

Testing Done:
Ran in `valgrind --leak-check=full` and confirmed that the leak was gone.

Reviewed at https://reviews.imfreedom.org/r/2298/
/* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "image-store.h"
#include "eventloop.h"
#include "util.h"
#define TEMP_IMAGE_TIMEOUT 5
static GHashTable *id_to_image = NULL;
static guint last_id = 0;
/* keys: timeout handle */
static GHashTable *temp_images = NULL;
/* keys: img id */
static GSList *perm_images = NULL;
static void
image_reset_id(gpointer _id)
{
g_return_if_fail(id_to_image != NULL);
g_hash_table_remove(id_to_image, _id);
}
static guint
image_set_id(PurpleImage *image)
{
/* Use the next unused id number. We do it in a loop on the off chance
* that next id wraps back around to 0 and the hash table still contains
* entries from the first time around.
*/
while (TRUE) {
last_id++;
if (G_UNLIKELY(last_id == 0))
continue;
if (purple_image_store_get(last_id) == NULL)
break;
}
g_object_set_data_full(G_OBJECT(image), "purple-image-store-id",
GINT_TO_POINTER(last_id), image_reset_id);
g_hash_table_insert(id_to_image, GINT_TO_POINTER(last_id), image);
return last_id;
}
static guint
image_get_id(PurpleImage *image)
{
return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(image),
"purple-image-store-id"));
}
guint
purple_image_store_add(PurpleImage *image)
{
guint id;
g_return_val_if_fail(PURPLE_IS_IMAGE(image), 0);
id = image_get_id(image);
if (id > 0)
return id;
id = image_set_id(image);
g_object_ref(image);
perm_images = g_slist_prepend(perm_images, image);
return id;
}
guint
purple_image_store_add_weak(PurpleImage *image)
{
guint id;
g_return_val_if_fail(PURPLE_IS_IMAGE(image), 0);
id = image_get_id(image);
if (id > 0)
return id;
return image_set_id(image);
}
static gboolean
remove_temporary(gpointer _image)
{
PurpleImage *image = _image;
guint handle;
handle = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(image),
"purple-image-store-handle"));
g_hash_table_remove(temp_images, GINT_TO_POINTER(handle));
return G_SOURCE_REMOVE;
}
static void
cancel_temporary(gpointer key, G_GNUC_UNUSED gpointer value,
G_GNUC_UNUSED gpointer data)
{
g_source_remove(GPOINTER_TO_INT(key));
}
guint
purple_image_store_add_temporary(PurpleImage *image)
{
guint id;
guint handle;
g_return_val_if_fail(PURPLE_IS_IMAGE(image), 0);
id = image_get_id(image);
/* XXX: add_temporary doesn't extend previous temporary call, sorry */
if (id > 0)
return id;
id = image_set_id(image);
g_object_ref(image);
handle = g_timeout_add_seconds(TEMP_IMAGE_TIMEOUT,
remove_temporary, image);
g_object_set_data(G_OBJECT(image), "purple-image-store-handle",
GINT_TO_POINTER(handle));
g_hash_table_insert(temp_images, GINT_TO_POINTER(handle), image);
return id;
}
PurpleImage *
purple_image_store_get(guint id)
{
return g_hash_table_lookup(id_to_image, GINT_TO_POINTER(id));
}
PurpleImage *
purple_image_store_get_from_uri(const gchar *uri)
{
guint64 longid;
guint id;
gchar *endptr;
gchar endchar;
g_return_val_if_fail(uri != NULL, NULL);
if (!g_str_has_prefix(uri, PURPLE_IMAGE_STORE_PROTOCOL)) {
return NULL;
}
uri += sizeof(PURPLE_IMAGE_STORE_PROTOCOL) - 1;
if (uri[0] == '-')
return NULL;
longid = g_ascii_strtoull(uri, &endptr, 10);
endchar = endptr[0];
if (endchar != '\0' && endchar != '"' &&
endchar != '\'' && endchar != ' ')
{
return NULL;
}
id = longid;
if (id != longid)
return NULL;
return purple_image_store_get(id);
}
gchar *
purple_image_store_get_uri(PurpleImage *image)
{
const gchar *path;
guint img_id;
g_return_val_if_fail(PURPLE_IS_IMAGE(image), NULL);
path = purple_image_get_path(image);
if (path)
return g_filename_to_uri(path, NULL, NULL);
img_id = purple_image_store_add_weak(image);
return g_strdup_printf(PURPLE_IMAGE_STORE_PROTOCOL "%u", img_id);
}
void
_purple_image_store_init(void)
{
id_to_image = g_hash_table_new(g_direct_hash, g_direct_equal);
temp_images = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, g_object_unref);
}
void
_purple_image_store_uninit(void)
{
g_slist_free_full(perm_images, g_object_unref);
perm_images = NULL;
g_hash_table_foreach(temp_images, cancel_temporary, NULL);
g_hash_table_destroy(temp_images);
temp_images = NULL;
g_hash_table_destroy(id_to_image);
id_to_image = NULL;
}