From bb21fc5e62c4c79b7fd8a4766e55cb80583c0c97 Mon Sep 17 00:00:00 2001 From: Vysheng Date: Thu, 11 Sep 2014 13:35:27 +0400 Subject: [PATCH] Fixed memory leak --- auto-static.c | 34 +++++++++++++++++++++++++++++++++- auto.h | 2 ++ generate.c | 1 + queries.c | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/auto-static.c b/auto-static.c index 2e32465..fe281cb 100644 --- a/auto-static.c +++ b/auto-static.c @@ -135,6 +135,20 @@ static struct paramed_type *paramed_type_dup (struct paramed_type *P) { return R; } +void tgl_paramed_type_free (struct paramed_type *P) { + if (ODDP (P)) { return; } + if (P->type->params_num) { + int i; + for (i = 0; i < P->type->params_num; i++) { + tgl_paramed_type_free (P->params[i]); + } + free (P->params); + } + free (P->type->id); + free (P->type); + free (P); +} + static void print_offset (void) { int i; for (i = 0; i < multiline_offset; i++) { @@ -277,6 +291,23 @@ static void local_next_token (void) { } } +#define MAX_FVARS 100 +static struct paramed_type *fvars[MAX_FVARS]; +static int fvars_pos; + +void add_var_to_be_freed (struct paramed_type *P) { + assert (fvars_pos < MAX_FVARS); + fvars[fvars_pos ++] = P; +} + +void free_vars_to_be_freed (void) { + int i; + for (i = 0; i < fvars_pos; i++) { + tgl_paramed_type_free (fvars[i]); + } + fvars_pos = 0; +} + int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, char *data, int data_len) { if (index == -1) { buffer_pos = data; @@ -284,6 +315,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, autocomplete_mode = 0; local_next_token (); autocomplete_function_any (); + free_vars_to_be_freed (); } if (autocomplete_mode == 0) { return -1; } int len = strlen (text); @@ -302,7 +334,7 @@ int tglf_extf_autocomplete (const char *text, int text_len, int index, char **R, } } -struct paramed_type *tglf_extf_store (const char *data, int data_len) { +struct paramed_type *tglf_extf_store (const char *data, int data_len) { buffer_pos = (char *)data; buffer_end = (char *)(data + data_len); local_next_token (); diff --git a/auto.h b/auto.h index 94aa6a4..f41bed0 100644 --- a/auto.h +++ b/auto.h @@ -41,6 +41,8 @@ struct paramed_type { #define INT2PTR(x) (void *)(long)(((long)x) * 2 + 1) #define PTR2INT(x) ((((long)x) - 1) / 2) +void tgl_paramed_type_free (struct paramed_type *P); + #include "auto/auto-header.h" #endif diff --git a/generate.c b/generate.c index 692b44d..09b48c2 100644 --- a/generate.c +++ b/generate.c @@ -713,6 +713,7 @@ int gen_field_autocomplete_excl (struct arg *arg, int *vars, int num, int from_f assert (t == NODE_TYPE_TYPE || t == NODE_TYPE_VAR_TYPE); printf ("%sstruct paramed_type *field%d = autocomplete_function_any ();\n", offset, num); printf ("%sif (!field%d) { return 0; }\n", offset, num); + printf ("%sadd_var_to_be_freed (field%d);\n", offset, num); static char s[20]; sprintf (s, "field%d", num); gen_uni_skip (arg->type, s, vars, 1, 1); diff --git a/queries.c b/queries.c index f0d0f82..54c0ca9 100644 --- a/queries.c +++ b/queries.c @@ -3475,6 +3475,7 @@ static int ext_query_on_answer (struct query *q UU) { char *buf = tglf_extf_fetch (q->type); ((void (*)(void *, int, char *))q->callback) (q->callback_extra, 1, buf); } + tgl_paramed_type_free (q->type); return 0; }