From 76814f7e7848e24ec90f3d50dfa8a6fd96e6153c Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 10 Jan 2009 16:35:33 -0500 Subject: [PATCH] Fix memory leak --- cmd/wmii/key.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/cmd/wmii/key.c b/cmd/wmii/key.c index 5bb1a8aa..2c55bb4f 100644 --- a/cmd/wmii/key.c +++ b/cmd/wmii/key.c @@ -1,4 +1,5 @@ -/* Copyright ©2004-2006 Anselm R. Garbe +/* Copyright ©2006-2008 Kris Maglione + * Copyright ©2004-2006 Anselm R. Garbe * See LICENSE file for license details. */ #include "dat.h" @@ -53,6 +54,16 @@ str2modmask(const char *val) { return mod; } +static void +freekey(Key *k) { + Key *n; + + while((n = k)) { + k = k->next; + free(n); + } +} + static void _grab(XWindow w, int keycode, uint mod) { XGrabKey(display, keycode, mod, w, @@ -61,7 +72,6 @@ _grab(XWindow w, int keycode, uint mod) { static void grabkey(Key *k) { - /* Round trip. */ _grab(scr.root.w, k->key, k->mod); if(numlock_mask) { _grab(scr.root.w, k->key, k->mod | numlock_mask); @@ -77,9 +87,7 @@ ungrabkey(Key *k) { XUngrabKey(display, k->key, k->mod | numlock_mask, scr.root.w); XUngrabKey(display, k->key, k->mod | numlock_mask | LockMask, scr.root.w); } - /* - sync(); - */ + /* sync(); */ } static Key * @@ -125,7 +133,10 @@ getkey(const char *name) { k->key = XKeysymToKeycode(display, XStringToKeysym(kstr)); k->mod = str2modmask(seq[i]); if (k->key == NoSymbol) + { + freekey(r); return nil; + } } if(r) { r->id = id++; @@ -215,7 +226,8 @@ kpress(XWindow w, ulong mod, KeyCode keycode) { Key *k, *found; for(k=key; k; k=k->lnext) - k->tnext=k->lnext; + k->tnext = k->lnext; + found = match_keys(key, mod, keycode, false); if(!found) /* grabbed but not found */ XBell(display, 0); @@ -232,17 +244,14 @@ kpress(XWindow w, ulong mod, KeyCode keycode) { void update_keys(void) { - Key *k, *n; + Key *k; char *l, *p; init_lock_keys(); while((k = key)) { key = key->lnext; ungrabkey(k); - while((n = k)) { - k = k->next; - free(n); - } + freekey(k); } for(l = p = def.keys; p && *p;) { if(*p == '\n') {