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:
parent
2a5014383d
commit
ea646e7077
|
@ -28,11 +28,13 @@
|
||||||
{ \
|
{ \
|
||||||
if(*item && --(*item)->refcount <= 0) \
|
if(*item && --(*item)->refcount <= 0) \
|
||||||
dtor(item); \
|
dtor(item); \
|
||||||
|
*item = NULL; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline void prefix##_ref(type **item) \
|
static inline type *prefix##_ref(type **item) \
|
||||||
{ \
|
{ \
|
||||||
(*item)->refcount++; \
|
(*item)->refcount++; \
|
||||||
|
return *item; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -103,10 +103,7 @@ luaA_keybinding_add(lua_State *L)
|
||||||
if(key == *k)
|
if(key == *k)
|
||||||
luaL_error(L, "keybinding already added");
|
luaL_error(L, "keybinding already added");
|
||||||
|
|
||||||
keybinding_list_push(&globalconf.keys, *k);
|
keybinding_list_push(&globalconf.keys, keybinding_ref(k));
|
||||||
|
|
||||||
keybinding_ref(k);
|
|
||||||
|
|
||||||
window_root_grabkey(*k);
|
window_root_grabkey(*k);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -124,10 +121,8 @@ luaA_keybinding_remove(lua_State *L)
|
||||||
keybinding_t **k = luaA_checkudata(L, 1, "keybinding");
|
keybinding_t **k = luaA_checkudata(L, 1, "keybinding");
|
||||||
|
|
||||||
keybinding_list_detach(&globalconf.keys, *k);
|
keybinding_list_detach(&globalconf.keys, *k);
|
||||||
|
|
||||||
keybinding_unref(k);
|
|
||||||
|
|
||||||
window_root_ungrabkey(*k);
|
window_root_ungrabkey(*k);
|
||||||
|
keybinding_unref(k);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue