Changeset b13daa0


Ignore:
Timestamp:
Mar 24, 2011, 4:09:22 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
45e2c95
Parents:
c809f5e
git-author:
David Benjamin <davidben@mit.edu> (03/10/11 15:14:32)
git-committer:
David Benjamin <davidben@mit.edu> (03/24/11 16:09:22)
Message:
Make owl_keybinding new/delete instead of init/cleanup

Valgrind is deeply disappointed in this code...

Hide owl_keybinding_make_keys again, so we don't construct half-baked
owl_keybinding instances. Instead allow for creating a dummy keybinding.
Also, initialize all fields of owl_keybinding, or freeing it afterwards
explodes. Also fix a memory leak in unbindkey.
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • keybinding.c

    rd4927a7 rb13daa0  
    1111 */
    1212
     13static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq);
     14
    1315/* sets up a new keybinding for a command */
    14 int owl_keybinding_init(owl_keybinding *kb, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
     16owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
    1517{
     18  owl_keybinding *kb = g_new(owl_keybinding, 1);
     19
    1620  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
    17   if (command && !function_fn) {
     21  if (command && function_fn) {
     22    g_free(kb);
     23    return NULL;
     24  } else if (command && !function_fn) {
    1825    kb->type = OWL_KEYBINDING_COMMAND;
    1926  } else if (!command && function_fn) {
    2027    kb->type = OWL_KEYBINDING_FUNCTION;
    2128  } else {
    22     return(-1);
     29    kb->type = OWL_KEYBINDING_NOOP;
    2330  }
    2431
    2532  if (owl_keybinding_make_keys(kb, keyseq) != 0) {
    26     return(-1);
     33    g_free(kb);
     34    return NULL;
    2735  }
    2836
    29   if (command) kb->command = g_strdup(command);
     37  kb->command = g_strdup(command);
    3038  kb->function_fn = function_fn;
    31   if (desc) kb->desc = g_strdup(desc);
    32   else kb->desc = NULL;
    33   return(0);
     39  kb->desc = g_strdup(desc);
     40  return kb;
    3441}
    3542
    36 int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
     43static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
    3744{
    3845  char **ktokens;
     
    5966}
    6067
    61 /* Releases data associated with a keybinding */
    62 void owl_keybinding_cleanup(owl_keybinding *kb)
     68/* Releases data associated with a keybinding, and the kb itself */
     69void owl_keybinding_delete(owl_keybinding *kb)
    6370{
    6471  if (kb->keys) g_free(kb->keys);
    6572  if (kb->desc) g_free(kb->desc);
    6673  if (kb->command) g_free(kb->command);
    67 }
    68 
    69 /* Releases data associated with a keybinding, and the kb itself */
    70 void owl_keybinding_delete(owl_keybinding *kb)
    71 {
    72   owl_keybinding_cleanup(kb);
    7374  g_free(kb);
    7475}
  • keymap.c

    rf25df21 rb13daa0  
    3838  int i;
    3939
    40   if ((kb = g_new(owl_keybinding, 1)) == NULL) return(-1);
    41   if (0 != owl_keybinding_init(kb, keyseq, command, function_fn, desc)) {
    42     g_free(kb);
    43     return(-1);
    44   }
     40  kb = owl_keybinding_new(keyseq, command, function_fn, desc);
     41  if (kb == NULL)
     42    return -1;
    4543  /* see if another matching binding, and if so remove it.
    4644   * otherwise just add this one.
     
    6361  int i;
    6462
    65   if ((kb = g_new(owl_keybinding, 1)) == NULL) return(-1);
    66   if (0 != owl_keybinding_make_keys(kb, keyseq)) {
    67     g_free(kb);
    68     return(-1);
    69   }
     63  kb = owl_keybinding_new(keyseq, NULL, NULL, NULL);
     64  if (kb == NULL)
     65    return -1;
    7066
    7167  for (i = owl_list_get_size(&km->bindings)-1; i >= 0; i--) {
     
    7470      owl_list_remove_element(&km->bindings, i);
    7571      owl_keybinding_delete(curkb);
     72      owl_keybinding_delete(kb);
    7673      return(0);
    7774    }
    7875  }
     76  owl_keybinding_delete(kb);
    7977  return(-2);
    8078}
  • owl.h

    r41f0cf3 rb13daa0  
    186186#define OWL_KEYMAP_MAXSTACK     20
    187187
     188#define OWL_KEYBINDING_NOOP     0   /* dummy binding that does nothing */
    188189#define OWL_KEYBINDING_COMMAND  1   /* command string */
    189190#define OWL_KEYBINDING_FUNCTION 2   /* function taking no args */
Note: See TracChangeset for help on using the changeset viewer.