From ea646e70778509a9812779f705a1e8f60ea7dc60 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 22 Jun 2008 14:12:19 +0200 Subject: [PATCH] Make refcounting safer. put *item to NULL on unref, as we cannot know if the pointer is valid after an unref, so just segfault rather than hide a problem. Also return *item on ref() it allow short versions like: foo_list_push(&list, foo_ref(&elem)); which is kind of readable _and_ handy. Signed-off-by: Pierre Habouzit --- common/refcount.h | 4 +++- keybinding.c | 9 ++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/common/refcount.h b/common/refcount.h index 18f4b109e..436f85307 100644 --- a/common/refcount.h +++ b/common/refcount.h @@ -28,11 +28,13 @@ { \ if(*item && --(*item)->refcount <= 0) \ dtor(item); \ + *item = NULL; \ } \ \ - static inline void prefix##_ref(type **item) \ + static inline type *prefix##_ref(type **item) \ { \ (*item)->refcount++; \ + return *item; \ } #endif diff --git a/keybinding.c b/keybinding.c index 97bfe0f6d..788189eaf 100644 --- a/keybinding.c +++ b/keybinding.c @@ -103,10 +103,7 @@ luaA_keybinding_add(lua_State *L) if(key == *k) luaL_error(L, "keybinding already added"); - keybinding_list_push(&globalconf.keys, *k); - - keybinding_ref(k); - + keybinding_list_push(&globalconf.keys, keybinding_ref(k)); window_root_grabkey(*k); return 0; @@ -124,10 +121,8 @@ luaA_keybinding_remove(lua_State *L) keybinding_t **k = luaA_checkudata(L, 1, "keybinding"); keybinding_list_detach(&globalconf.keys, *k); - - keybinding_unref(k); - window_root_ungrabkey(*k); + keybinding_unref(k); return 0; }