Merge pull request #115 from RichardHitt/master

Fix memory leak related to url grabbing
This commit is contained in:
bviktor 2012-10-02 13:02:17 -07:00
commit c80858f919
4 changed files with 23 additions and 12 deletions

View File

@ -744,7 +744,7 @@ load_config (void)
prefs.input_tray_priv = prefs.input_tray_hilight = 1; prefs.input_tray_priv = prefs.input_tray_hilight = 1;
prefs.autodccsend = 2; /* browse mode */ prefs.autodccsend = 2; /* browse mode */
prefs.url_grabber = 1; prefs.url_grabber = 1;
prefs.url_grabber_limit = 50; /* 0 means unlimited */ prefs.url_grabber_limit = 0; /* 0 means unlimited */
prefs.text_search_follow = 1; prefs.text_search_follow = 1;
#ifdef WIN32 #ifdef WIN32
prefs.identd = 1; prefs.identd = 1;

View File

@ -150,10 +150,11 @@ tree_find (tree *t, void *key, tree_cmp_func *cmp, void *data, int *pos)
return mybsearch (key, &t->array[0], t->elements, cmp, data, pos); return mybsearch (key, &t->array[0], t->elements, cmp, data, pos);
} }
void void *
tree_remove_at_pos (tree *t, int pos) tree_remove_at_pos (tree *t, int pos)
{ {
int post_bytes; int post_bytes;
void *ret = t->array[pos];
t->elements--; t->elements--;
if (pos != t->elements) if (pos != t->elements)
@ -161,6 +162,7 @@ tree_remove_at_pos (tree *t, int pos)
post_bytes = (t->elements - pos) * sizeof (void *); post_bytes = (t->elements - pos) * sizeof (void *);
memmove (&t->array[pos], &t->array[pos + 1], post_bytes); memmove (&t->array[pos], &t->array[pos + 1], post_bytes);
} }
return ret;
} }
int int

View File

@ -10,7 +10,7 @@ tree *tree_new (tree_cmp_func *cmp, void *data);
void tree_destroy (tree *t); void tree_destroy (tree *t);
void *tree_find (tree *t, void *key, tree_cmp_func *cmp, void *data, int *pos); void *tree_find (tree *t, void *key, tree_cmp_func *cmp, void *data, int *pos);
int tree_remove (tree *t, void *key, int *pos); int tree_remove (tree *t, void *key, int *pos);
void tree_remove_at_pos (tree *t, int pos); void *tree_remove_at_pos (tree *t, int pos);
void tree_foreach (tree *t, tree_traverse_func *func, void *data); void tree_foreach (tree *t, tree_traverse_func *func, void *data);
int tree_insert (tree *t, void *key); int tree_insert (tree *t, void *key);
void tree_append (tree* t, void *key); void tree_append (tree* t, void *key);

View File

@ -31,6 +31,7 @@
#endif #endif
void *url_tree = NULL; void *url_tree = NULL;
GTree *url_btree = NULL;
static int static int
@ -46,6 +47,8 @@ url_clear (void)
tree_foreach (url_tree, (tree_traverse_func *)url_free, NULL); tree_foreach (url_tree, (tree_traverse_func *)url_free, NULL);
tree_destroy (url_tree); tree_destroy (url_tree);
url_tree = NULL; url_tree = NULL;
g_tree_destroy (url_btree);
url_btree = NULL;
} }
static int static int
@ -80,11 +83,7 @@ url_autosave (void)
static int static int
url_find (char *urltext) url_find (char *urltext)
{ {
int pos; return (g_tree_lookup_extended (url_btree, urltext, NULL, NULL));
if (tree_find (url_tree, urltext, (tree_cmp_func *)g_ascii_strcasecmp, NULL, &pos))
return 1;
return 0;
} }
static void static void
@ -110,15 +109,18 @@ url_add (char *urltext, int len)
if (data[len - 1] == ')') /* chop trailing ) */ if (data[len - 1] == ')') /* chop trailing ) */
data[len - 1] = 0; data[len - 1] = 0;
if (!url_tree)
{
url_tree = tree_new ((tree_cmp_func *)strcasecmp, NULL);
url_btree = g_tree_new ((GCompareFunc)strcasecmp);
}
if (url_find (data)) if (url_find (data))
{ {
free (data); free (data);
return; return;
} }
if (!url_tree)
url_tree = tree_new ((tree_cmp_func *)g_ascii_strcasecmp, NULL);
size = tree_size (url_tree); size = tree_size (url_tree);
/* 0 is unlimited */ /* 0 is unlimited */
if (prefs.url_grabber_limit > 0 && size >= prefs.url_grabber_limit) if (prefs.url_grabber_limit > 0 && size >= prefs.url_grabber_limit)
@ -127,10 +129,17 @@ url_add (char *urltext, int len)
xchat is running */ xchat is running */
size -= prefs.url_grabber_limit; size -= prefs.url_grabber_limit;
for(; size > 0; size--) for(; size > 0; size--)
tree_remove_at_pos (url_tree, 0); {
char *pos;
pos = tree_remove_at_pos (url_tree, 0);
g_tree_remove (url_btree, pos);
free (pos);
}
} }
tree_append (url_tree, data); tree_append (url_tree, data);
g_tree_insert (url_btree, data, GINT_TO_POINTER (tree_size (url_tree) - 1));
fe_url_add (data); fe_url_add (data);
} }