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 <madcoder@debian.org>
This commit is contained in:
Pierre Habouzit 2008-06-22 14:12:19 +02:00
parent 2a5014383d
commit ea646e7077
2 changed files with 5 additions and 8 deletions

View File

@ -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

View File

@ -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;
}