From dc790cfa32e17cf975060f6575eead9b0bc3cfdb Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 11 Sep 2011 23:41:46 +0100 Subject: [PATCH] Bugfix: Correctly free old assignments when reloading Fixes #516 --- include/data.h | 2 +- include/match.h | 6 ++++++ include/regex.h | 6 ++++++ src/config.c | 10 +++++++--- src/match.c | 20 ++++++++++++++++++++ src/regex.c | 12 ++++++++++++ 6 files changed, 52 insertions(+), 4 deletions(-) diff --git a/include/data.h b/include/data.h index 122833c3..1db7c442 100644 --- a/include/data.h +++ b/include/data.h @@ -148,7 +148,7 @@ struct Ignore_Event { * */ struct regex { - const char *pattern; + char *pattern; pcre *regex; pcre_extra *extra; }; diff --git a/include/match.h b/include/match.h index 2786c66a..6c0694ef 100644 --- a/include/match.h +++ b/include/match.h @@ -28,4 +28,10 @@ void match_copy(Match *dest, Match *src); */ bool match_matches_window(Match *match, i3Window *window); +/** + * Frees the given match. It must not be used afterwards! + * + */ +void match_free(Match *match); + #endif diff --git a/include/regex.h b/include/regex.h index 2a6608a8..adfa6656 100644 --- a/include/regex.h +++ b/include/regex.h @@ -17,6 +17,12 @@ */ struct regex *regex_new(const char *pattern); +/** + * Frees the given regular expression. It must not be used afterwards! + * + */ +void regex_free(struct regex *regex); + /** * Checks if the given regular expression matches the given input and returns * true if it does. In either case, it logs the outcome using LOG(), so it will diff --git a/src/config.c b/src/config.c index 5dc52472..c979d8cd 100644 --- a/src/config.c +++ b/src/config.c @@ -280,15 +280,19 @@ void load_configuration(xcb_connection_t *conn, const char *override_configpath, SLIST_REMOVE(&modes, mode, Mode, modes); } -#if 0 struct Assignment *assign; while (!TAILQ_EMPTY(&assignments)) { assign = TAILQ_FIRST(&assignments); - FREE(assign->windowclass_title); + if (assign->type == A_TO_WORKSPACE) + FREE(assign->dest.workspace); + else if (assign->type == A_TO_OUTPUT) + FREE(assign->dest.output); + else if (assign->type == A_COMMAND) + FREE(assign->dest.command); + match_free(&(assign->match)); TAILQ_REMOVE(&assignments, assign, assignments); FREE(assign); } -#endif /* Clear workspace names */ #if 0 diff --git a/src/match.c b/src/match.c index 85d2eaab..3514acee 100644 --- a/src/match.c +++ b/src/match.c @@ -136,3 +136,23 @@ bool match_matches_window(Match *match, i3Window *window) { return true; } + +/* + * Frees the given match. It must not be used afterwards! + * + */ +void match_free(Match *match) { + /* First step: free the regex fields / patterns */ + regex_free(match->title); + regex_free(match->application); + regex_free(match->class); + regex_free(match->instance); + regex_free(match->mark); + + /* Second step: free the regex helper struct itself */ + FREE(match->title); + FREE(match->application); + FREE(match->class); + FREE(match->instance); + FREE(match->mark); +} diff --git a/src/regex.c b/src/regex.c index da8f91d8..f419e4bb 100644 --- a/src/regex.c +++ b/src/regex.c @@ -49,6 +49,18 @@ struct regex *regex_new(const char *pattern) { return re; } +/* + * Frees the given regular expression. It must not be used afterwards! + * + */ +void regex_free(struct regex *regex) { + if (!regex) + return; + FREE(regex->pattern); + FREE(regex->regex); + FREE(regex->extra); +} + /* * Checks if the given regular expression matches the given input and returns * true if it does. In either case, it logs the outcome using LOG(), so it will