From bf27651edee3b143fc70d2f05ee93e6a560afb1d Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Tue, 5 Mar 2013 14:00:18 +0100 Subject: [PATCH] Fixed some memory leaks in completion and dom handling. --- src/completion.c | 14 ++++++++++++-- src/dom.c | 36 ++++++++++++++++++++++++------------ src/hints.c | 3 --- src/main.c | 4 +++- src/url_history.c | 7 +++++++ 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/completion.c b/src/completion.c index fba9daa..d34410f 100644 --- a/src/completion.c +++ b/src/completion.c @@ -35,6 +35,7 @@ static void completion_show(Client* c, gboolean back); static void completion_set_entry_text(Client* c, Completion* completion); static char* completion_get_text(Client* c, Completion* completion); static Completion* completion_get_new(const char* label, const char* prefix); +static void completion_free(Completion* completion); gboolean completion_complete(Client* c, gboolean back) { @@ -49,9 +50,10 @@ gboolean completion_complete(Client* c, gboolean back) if (!strcmp(input, text)) { /* updatecompletions */ c->comps.active = completion_update(c, c->comps.completions, c->comps.active, back); - + g_free(text); return TRUE; } else { + g_free(text); /* if current input isn't the content of the completion item */ completion_clean(c); } @@ -80,6 +82,7 @@ gboolean completion_complete(Client* c, gboolean back) c, c->comps.completions, source, (Comp_Func)util_strcasestr, &input[6], ":open " ); + g_list_free(source); } else if (!strncmp(input, ":tabopen ", 9)) { source = url_history_get_all(); c->comps.completions = completion_init_completion( @@ -105,7 +108,7 @@ gboolean completion_complete(Client* c, gboolean back) void completion_clean(Client* c) { - g_list_free_full(c->comps.completions, (GDestroyNotify)g_free); + g_list_free_full(c->comps.completions, (GDestroyNotify)completion_free); c->comps.completions = NULL; if (c->gui.compbox) { @@ -318,3 +321,10 @@ static Completion* completion_get_new(const char* label, const char* prefix) return c; } + +static void completion_free(Completion* completion) +{ + gtk_widget_destroy(completion->event); + g_free(completion->prefix); + g_free(completion); +} diff --git a/src/dom.c b/src/dom.c index b8f1711..260c506 100644 --- a/src/dom.c +++ b/src/dom.c @@ -46,24 +46,28 @@ void dom_check_auto_insert(Client* c) */ gboolean dom_is_editable(Element* element) { + gboolean result = FALSE; if (!element) { - return FALSE; + return result; } char* tagname = webkit_dom_element_get_tag_name(element); - if (!g_ascii_strcasecmp(tagname, "textarea")) { - return TRUE; - } char *type = webkit_dom_element_get_attribute(element, "type"); - if (!g_ascii_strcasecmp(tagname, "input") + if (!g_ascii_strcasecmp(tagname, "textarea")) { + result = TRUE; + } else if (!g_ascii_strcasecmp(tagname, "input") && g_ascii_strcasecmp(type, "submit") && g_ascii_strcasecmp(type, "reset") && g_ascii_strcasecmp(type, "image") ) { - return TRUE; + result = TRUE; + } else { + result = FALSE; } + g_free(tagname); + g_free(type); - return FALSE; + return result; } static gboolean dom_auto_insert(Client* c, Element* element) @@ -91,15 +95,23 @@ static Element* dom_get_active_element(Document* doc) { Document* d = NULL; Element* active = webkit_dom_html_document_get_active_element((void*)doc); - char* tagname = webkit_dom_element_get_tag_name(active); + char* tagname = webkit_dom_element_get_tag_name(active); + Element* result = NULL; if (!g_strcmp0(tagname, "FRAME")) { d = webkit_dom_html_frame_element_get_content_document(WEBKIT_DOM_HTML_FRAME_ELEMENT(active)); - return dom_get_active_element(d); - } - if (!g_strcmp0(tagname, "IFRAME")) { + result = dom_get_active_element(d); + } else if (!g_strcmp0(tagname, "IFRAME")) { d = webkit_dom_html_iframe_element_get_content_document(WEBKIT_DOM_HTML_IFRAME_ELEMENT(active)); - return dom_get_active_element(d); + result = dom_get_active_element(d); } + g_free(tagname); + + if (result) { + g_free(active); + + return result; + } + return active; } diff --git a/src/hints.c b/src/hints.c index e65b347..8e4314e 100644 --- a/src/hints.c +++ b/src/hints.c @@ -128,9 +128,6 @@ static void hints_run_script(Client* c, char* js) return; } - if (!value) { - return; - } if (!strncmp(value, "OVER:", 5)) { g_signal_emit_by_name( diff --git a/src/main.c b/src/main.c index 90d4335..3cf9a89 100644 --- a/src/main.c +++ b/src/main.c @@ -710,8 +710,8 @@ static void vp_process_config_file(Client* c, VpFile file) fprintf(stderr, "Invalid config: %s\n", line); } } - g_strfreev(lines); } + g_strfreev(lines); } static Client* vp_client_new(void) @@ -1035,6 +1035,8 @@ static void vp_destroy_client(Client* c) webkit_web_view_stop_loading(c->gui.webview); gtk_widget_destroy(GTK_WIDGET(c->gui.webview)); + gtk_widget_destroy(GTK_WIDGET(c->gui.scroll)); + gtk_widget_destroy(GTK_WIDGET(c->gui.box)); gtk_widget_destroy(GTK_WIDGET(c->gui.window)); for(p = clients; p && p->next != c; p = p->next); diff --git a/src/url_history.c b/src/url_history.c index 1381c5d..1c0f729 100644 --- a/src/url_history.c +++ b/src/url_history.c @@ -27,6 +27,9 @@ void url_history_init(void) file_lock_set(fileno(file), F_UNLCK); fclose(file); + + /* reverse the history because we read it from lates to old from file */ + core.behave.url_history = g_list_reverse(core.behave.url_history); } void url_history_cleanup(void) @@ -83,6 +86,9 @@ void url_history_add(const char* url, const char* title) core.behave.url_history = g_list_prepend(core.behave.url_history, item); } +/** + * Retrieves the ur history as ne allocated list. + */ GList* url_history_get_all(void) { GList* out = NULL; @@ -102,4 +108,5 @@ static void url_history_free(UrlHist* item) if (item->title) { g_free(item->title); } + g_free(item); } -- 2.20.1