From 46898b10902b3ad635a441bbb87ccee103ecc6ca Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sun, 2 Dec 2012 13:56:31 +0100 Subject: [PATCH] shl: hook: allow callbacks to destroy the hook If we currently dispatch a callback and they unregister themself and then destroy the hook, we currently do not free the hook as we would free memory that is used in the callstack. However, it is a valid use-case for hook-callbacks to destroy the hook. Therefore, we set a "dead" flag and destroy the hook after all callbacks are done. This fixes some memory-leaks inside the shared-signal callbacks of eloop. Signed-off-by: David Herrmann --- src/shl_hook.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/shl_hook.h b/src/shl_hook.h index b038bd6..aa35eb3 100644 --- a/src/shl_hook.h +++ b/src/shl_hook.h @@ -58,6 +58,7 @@ struct shl_hook { unsigned int num; struct shl_dlist entries; struct shl_dlist *cur_entry; + bool dead; }; static inline int shl_hook_new(struct shl_hook **out) @@ -81,9 +82,14 @@ static inline void shl_hook_free(struct shl_hook *hook) { struct shl_hook_entry *entry; - if (!hook || hook->cur_entry) + if (!hook) return; + if (hook->cur_entry) { + hook->dead = true; + return; + } + while (!shl_dlist_empty(&hook->entries)) { entry = shl_dlist_entry(hook->entries.prev, struct shl_hook_entry, @@ -164,6 +170,8 @@ static inline void shl_hook_call(struct shl_hook *hook, void *parent, } hook->cur_entry = NULL; + if (hook->dead) + shl_hook_free(hook); } #endif /* SHL_HOOK_H */ -- 2.7.4