[2956] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[PATCH] Don't call owl_list_create in owl_dict_get_keys

daemon@ATHENA.MIT.EDU (David Benjamin)
Tue Mar 8 15:18:57 2011

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Tue,  8 Mar 2011 15:18:38 -0500

Until we get rid of this owl_list thing altogether, there should be a
convention as to who initializes the thing. Otherwise, we leak memory
from people initializing it too many times.

Whoever reviews this probably wants to look over this very carefully in
case I missed one of the owl_list_creates.
---
 dict.c       |    3 +--
 functions.c  |    5 +++++
 help.c       |    1 +
 perlconfig.c |    1 +
 perlglue.xs  |    1 +
 tester.c     |    1 +
 6 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/dict.c b/dict.c
index 2d245d0..e5dbb95 100644
--- a/dict.c
+++ b/dict.c
@@ -57,12 +57,11 @@ void *owl_dict_find_element(const owl_dict *d, const char *k) {
   return(d->els[pos].v);
 }
 
-/* creates a list and fills it in with keys.  duplicates the keys, 
+/* Appends dictionary keys to a list.  Duplicates the keys,
  * so they will need to be freed by the caller. */
 int owl_dict_get_keys(const owl_dict *d, owl_list *l) {
   int i;
   char *dupk;
-  if (owl_list_create(l)) return(-1);
   for (i=0; i<d->size; i++) {
     if ((dupk = g_strdup(d->els[i].k)) == NULL) return(-1);
     owl_list_append_element(l, dupk);
diff --git a/functions.c b/functions.c
index 2863f65..c831176 100644
--- a/functions.c
+++ b/functions.c
@@ -52,6 +52,7 @@ void owl_function_show_commands(void)
   owl_fmtext_init_null(&fm);
   owl_fmtext_append_bold(&fm, "Commands:  ");
   owl_fmtext_append_normal(&fm, "(use 'show command <name>' for details)\n");
+  owl_list_create(&l);
   owl_cmddict_get_names(owl_global_get_cmddict(&g), &l);
   owl_fmtext_append_list(&fm, &l, "\n", owl_function_cmd_describe);
   owl_fmtext_append_normal(&fm, "\n");
@@ -84,6 +85,7 @@ void owl_function_show_styles(void) {
 
   owl_fmtext_init_null(&fm);
   owl_fmtext_append_bold(&fm, "Styles:\n");
+  owl_list_create(&l);
   owl_global_get_style_names(&g, &l);
   owl_fmtext_append_list(&fm, &l, "\n", owl_function_style_describe);
   owl_fmtext_append_normal(&fm, "\n");
@@ -1622,6 +1624,7 @@ void owl_function_printallvars(void)
 
   g_string_append_printf(str, "%-20s = %s\n", "VARIABLE", "VALUE");
   g_string_append_printf(str, "%-20s   %s\n",  "--------", "-----");
+  owl_list_create(&varnames);
   owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
   numvarnames = owl_list_get_size(&varnames);
   for (i=0; i<numvarnames; i++) {
@@ -1652,6 +1655,7 @@ void owl_function_show_variables(void)
   owl_fmtext_init_null(&fm);
   owl_fmtext_append_bold(&fm, 
       "Variables: (use 'show variable <name>' for details)\n");
+  owl_list_create(&varnames);
   owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
   numvarnames = owl_list_get_size(&varnames);
   for (i=0; i<numvarnames; i++) {
@@ -2870,6 +2874,7 @@ void owl_function_show_keymaps(void)
   owl_fmtext_init_null(&fm);
   owl_fmtext_append_bold(&fm, "Keymaps:   ");
   owl_fmtext_append_normal(&fm, "(use 'show keymap <name>' for details)\n");
+  owl_list_create(&l);
   owl_keyhandler_get_keymap_names(kh, &l);
   owl_fmtext_append_list(&fm, &l, "\n", owl_function_keymap_summary);
   owl_fmtext_append_normal(&fm, "\n");
diff --git a/help.c b/help.c
index 20cb5ea..ea35f8c 100644
--- a/help.c
+++ b/help.c
@@ -128,6 +128,7 @@ void owl_help(void)
   /* help for variables */
   owl_fmtext_append_bold(&fm, 
 			 "Variables:\n");
+  owl_list_create(&varnames);
   owl_variable_dict_get_names(owl_global_get_vardict(&g), &varnames);
   numvarnames = owl_list_get_size(&varnames);
   for (i=0; i<numvarnames; i++) {
diff --git a/perlconfig.c b/perlconfig.c
index e895c06..a974bd1 100644
--- a/perlconfig.c
+++ b/perlconfig.c
@@ -62,6 +62,7 @@ HV *owl_new_hv(const owl_dict *d, SV *(*to_sv)(const void *))
   ret = newHV();
 
   /* TODO: add an iterator-like interface to owl_dict */
+  owl_list_create(&l);
   owl_dict_get_keys(d, &l);
   for (i = 0; i < owl_list_get_size(&l); i++) {
     key = owl_list_get_element(&l, i);
diff --git a/perlglue.xs b/perlglue.xs
index e5e04d4..8c442d1 100644
--- a/perlglue.xs
+++ b/perlglue.xs
@@ -388,6 +388,7 @@ all_keymaps()
 	CODE:
 	{
 		kh = owl_global_get_keyhandler(&g);
+		owl_list_create(&l);
 		owl_keyhandler_get_keymap_names(kh, &l);
 		RETVAL = owl_new_av(&l, (SV*(*)(const void*))owl_new_sv);
 		sv_2mortal((SV*)RETVAL);
diff --git a/tester.c b/tester.c
index 2951eaf..4d6e70e 100644
--- a/tester.c
+++ b/tester.c
@@ -251,6 +251,7 @@ int owl_dict_regtest(void) {
   FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
 
   FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
+  owl_list_create(&l);
   FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
   FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
   
-- 
1.7.3.3.399.g06ddf


home help back first fref pref prev next nref lref last post