Mercurial > pidgin
comparison libpurple/buddyicon.c @ 16383:05033ae856b2
Fix the memory leaking and improper calls to ref_filename(). Also, remove some debugging code.
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Tue, 24 Apr 2007 19:12:48 +0000 |
parents | 72dc611f3257 |
children | 54ea8863b8e8 |
comparison
equal
deleted
inserted
replaced
16382:5f6abf3a5369 | 16383:05033ae856b2 |
---|---|
60 int refs; | 60 int refs; |
61 | 61 |
62 g_return_if_fail(filename != NULL); | 62 g_return_if_fail(filename != NULL); |
63 | 63 |
64 refs = GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename)); | 64 refs = GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename)); |
65 printf("refs before increment = %d\n", refs); | |
66 | 65 |
67 g_hash_table_insert(icon_file_cache, g_strdup(filename), | 66 g_hash_table_insert(icon_file_cache, g_strdup(filename), |
68 GINT_TO_POINTER(refs + 1)); | 67 GINT_TO_POINTER(refs + 1)); |
69 } | 68 } |
70 | 69 |
75 | 74 |
76 if (filename == NULL) | 75 if (filename == NULL) |
77 return; | 76 return; |
78 | 77 |
79 refs = GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename)); | 78 refs = GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename)); |
80 printf("refs before decrement = %d\n", refs); | |
81 | 79 |
82 if (refs == 1) | 80 if (refs == 1) |
83 { | 81 { |
84 g_hash_table_remove(icon_file_cache, filename); | 82 g_hash_table_remove(icon_file_cache, filename); |
85 } | 83 } |
174 | 172 |
175 g_return_if_fail(filename != NULL); | 173 g_return_if_fail(filename != NULL); |
176 | 174 |
177 /* It's possible that there are other references to this icon | 175 /* It's possible that there are other references to this icon |
178 * cache file that are not currently loaded into memory. */ | 176 * cache file that are not currently loaded into memory. */ |
179 printf("file has %d refs\n", GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename))); | |
180 if (GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename))) | 177 if (GPOINTER_TO_INT(g_hash_table_lookup(icon_file_cache, filename))) |
181 return; | 178 return; |
182 | 179 |
183 dirname = purple_buddy_icons_get_cache_dir(); | 180 dirname = purple_buddy_icons_get_cache_dir(); |
184 path = g_build_filename(dirname, filename, NULL); | 181 path = g_build_filename(dirname, filename, NULL); |
185 | 182 |
186 printf("Going to unlink %s\n", path); | |
187 if (g_file_test(path, G_FILE_TEST_EXISTS)) | 183 if (g_file_test(path, G_FILE_TEST_EXISTS)) |
188 { | 184 { |
189 if (g_unlink(path)) | 185 if (g_unlink(path)) |
190 { | 186 { |
191 printf("Failed to unlink %s\n", path); | |
192 | |
193 purple_debug_error("buddyicon", "Failed to delete %s: %s\n", | 187 purple_debug_error("buddyicon", "Failed to delete %s: %s\n", |
194 path, strerror(errno)); | 188 path, strerror(errno)); |
195 } | 189 } |
196 else | 190 else |
197 { | 191 { |
198 printf("Unlinked %s\n", path); | |
199 purple_debug_info("buddyicon", "Deleted cache file: %s\n", path); | 192 purple_debug_info("buddyicon", "Deleted cache file: %s\n", path); |
200 } | 193 } |
201 } | 194 } |
202 | 195 |
203 g_free(path); | 196 g_free(path); |
197 } | |
198 | |
199 gboolean | |
200 value_equals(gpointer key, gpointer value, gpointer user_data) | |
201 { | |
202 return (value == user_data); | |
204 } | 203 } |
205 | 204 |
206 static void | 205 static void |
207 image_deleting_cb(PurpleStoredImage *img, gpointer data) | 206 image_deleting_cb(PurpleStoredImage *img, gpointer data) |
208 { | 207 { |
210 | 209 |
211 if (img == g_hash_table_lookup(icon_data_cache, filename)) | 210 if (img == g_hash_table_lookup(icon_data_cache, filename)) |
212 { | 211 { |
213 purple_buddy_icon_data_uncache_file(filename); | 212 purple_buddy_icon_data_uncache_file(filename); |
214 g_hash_table_remove(icon_data_cache, filename); | 213 g_hash_table_remove(icon_data_cache, filename); |
214 | |
215 /* We could make this O(1) by using another hash table, but | |
216 * this is probably good enough. */ | |
217 g_hash_table_foreach_remove(custom_icon_cache, value_equals, img); | |
215 } | 218 } |
216 } | 219 } |
217 | 220 |
218 static PurpleStoredImage * | 221 static PurpleStoredImage * |
219 purple_buddy_icon_data_new(guchar *icon_data, size_t icon_len, const char *filename) | 222 purple_buddy_icon_data_new(guchar *icon_data, size_t icon_len, const char *filename) |
383 { | 386 { |
384 const char *filename = purple_imgstore_get_filename(icon->img); | 387 const char *filename = purple_imgstore_get_filename(icon->img); |
385 purple_blist_node_set_string((PurpleBlistNode *)buddy, | 388 purple_blist_node_set_string((PurpleBlistNode *)buddy, |
386 "buddy_icon", | 389 "buddy_icon", |
387 filename); | 390 filename); |
388 printf("Calling ref_filename(%s)\n", filename); | |
389 ref_filename(filename); | 391 ref_filename(filename); |
390 } | 392 } |
391 else | 393 else |
392 { | 394 { |
393 purple_blist_node_remove_setting((PurpleBlistNode *)buddy, "buddy_icon"); | 395 purple_blist_node_remove_setting((PurpleBlistNode *)buddy, "buddy_icon"); |
394 } | 396 } |
395 printf("Calling unref_filename(%s)\n", old_icon); | |
396 unref_filename(old_icon); | 397 unref_filename(old_icon); |
397 g_free(old_icon); | 398 g_free(old_icon); |
398 } | 399 } |
399 | 400 |
400 g_slist_free(list); | 401 g_slist_free(list); |
416 icon->img = NULL; | 417 icon->img = NULL; |
417 | 418 |
418 if (data != NULL && len > 0) | 419 if (data != NULL && len > 0) |
419 icon->img = purple_buddy_icon_data_new(data, len, NULL); | 420 icon->img = purple_buddy_icon_data_new(data, len, NULL); |
420 | 421 |
421 printf("Calling purple_buddy_icon_update\n"); | |
422 purple_buddy_icon_update(icon); | 422 purple_buddy_icon_update(icon); |
423 | 423 |
424 printf("Calling purple_imgstore_unref\n"); | |
425 purple_imgstore_unref(old_img); | 424 purple_imgstore_unref(old_img); |
426 } | 425 } |
427 | 426 |
428 PurpleAccount * | 427 PurpleAccount * |
429 purple_buddy_icon_get_account(const PurpleBuddyIcon *icon) | 428 purple_buddy_icon_get_account(const PurpleBuddyIcon *icon) |
813 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) | 812 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) |
814 { | 813 { |
815 purple_blist_node_remove_setting(node, | 814 purple_blist_node_remove_setting(node, |
816 "buddy_icon"); | 815 "buddy_icon"); |
817 } | 816 } |
817 else | |
818 ref_filename(filename); | |
818 g_free(path); | 819 g_free(path); |
819 ref_filename(filename); | |
820 } | 820 } |
821 } | 821 } |
822 } | 822 } |
823 else if (PURPLE_BLIST_NODE_IS_CONTACT(node)) | 823 else if (PURPLE_BLIST_NODE_IS_CONTACT(node)) |
824 { | 824 { |
839 if (!g_file_test(path, G_FILE_TEST_EXISTS)) | 839 if (!g_file_test(path, G_FILE_TEST_EXISTS)) |
840 { | 840 { |
841 purple_blist_node_remove_setting(node, | 841 purple_blist_node_remove_setting(node, |
842 "custom_buddy_icon"); | 842 "custom_buddy_icon"); |
843 } | 843 } |
844 else | |
845 ref_filename(filename); | |
844 g_free(path); | 846 g_free(path); |
845 ref_filename(filename); | |
846 } | 847 } |
847 } | 848 } |
848 } | 849 } |
849 node = purple_blist_node_next(node, TRUE); | 850 node = purple_blist_node_next(node, TRUE); |
850 } | 851 } |