shl: hook: allow callbacks to destroy the hook
authorDavid Herrmann <dh.herrmann@googlemail.com>
Sun, 2 Dec 2012 12:56:31 +0000 (13:56 +0100)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Sun, 2 Dec 2012 12:56:31 +0000 (13:56 +0100)
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 <dh.herrmann@googlemail.com>
src/shl_hook.h

index b038bd6..aa35eb3 100644 (file)
@@ -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 */