提交 2306ad46 编写于 作者: B Behdad Esfahbod

[util] Fix memory issue

上级 14a4a9d6
...@@ -371,15 +371,15 @@ void ...@@ -371,15 +371,15 @@ void
output_options_t::add_options (option_parser_t *parser) output_options_t::add_options (option_parser_t *parser)
{ {
const char *text; const char *text;
char *text_free = NULL;
if (NULL == supported_formats) if (NULL == supported_formats)
text = "Set output format"; text = "Set output format";
else else
{ {
char *items = g_strjoinv ("/", const_cast<char **> (supported_formats)); char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
text = text_free = g_strdup_printf ("Set output format\n\n Supported formats are: %s", items); text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items);
g_free (items); g_free (items);
parser->free_later ((char *) text);
} }
GOptionEntry entries[] = GOptionEntry entries[] =
...@@ -393,8 +393,6 @@ output_options_t::add_options (option_parser_t *parser) ...@@ -393,8 +393,6 @@ output_options_t::add_options (option_parser_t *parser)
"Output options:", "Output options:",
"Options controlling the output", "Options controlling the output",
this); this);
g_free (text_free);
} }
......
...@@ -85,11 +85,14 @@ struct option_parser_t ...@@ -85,11 +85,14 @@ struct option_parser_t
memset (this, 0, sizeof (*this)); memset (this, 0, sizeof (*this));
usage_str = usage; usage_str = usage;
context = g_option_context_new (usage); context = g_option_context_new (usage);
to_free = g_ptr_array_new ();
add_main_options (); add_main_options ();
} }
~option_parser_t (void) { ~option_parser_t (void) {
g_option_context_free (context); g_option_context_free (context);
g_ptr_array_foreach (to_free, (GFunc) g_free, NULL);
g_ptr_array_free (to_free, TRUE);
} }
void add_main_options (void); void add_main_options (void);
...@@ -100,6 +103,10 @@ struct option_parser_t ...@@ -100,6 +103,10 @@ struct option_parser_t
const gchar *help_description, const gchar *help_description,
option_group_t *option_group); option_group_t *option_group);
void free_later (char *p) {
g_ptr_array_add (to_free, p);
}
void parse (int *argc, char ***argv); void parse (int *argc, char ***argv);
G_GNUC_NORETURN void usage (void) { G_GNUC_NORETURN void usage (void) {
...@@ -107,8 +114,10 @@ struct option_parser_t ...@@ -107,8 +114,10 @@ struct option_parser_t
exit (1); exit (1);
} }
private:
const char *usage_str; const char *usage_str;
GOptionContext *context; GOptionContext *context;
GPtrArray *to_free;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册