diff --git a/callback.c b/callback.c index b3b65cca..54c4f816 100644 --- a/callback.c +++ b/callback.c @@ -135,37 +135,28 @@ static void markUserdata4Kill(pICallBack self, void *pData) /*-------------------------------------------------------------------------*/ static void cleanCallbackList(pICallBack self) { - pCallBackItem toKill, current; + pCallBackItem toKill, current, previous; - /* - * killing at the head - */ - while (self->head != NULL && self->head->killFlag == 0) { - toKill = self->head; - self->head = toKill->next; - if (toKill->pKill != NULL) { - toKill->pKill(toKill->pUserData); - } - free(toKill); - } - - if (self->head == NULL) { - return; - } - - /* - * killing in the middle and the end - */ + previous = NULL; current = self->head; - while (current->next != NULL) { - if (current->next->killFlag == 1) { - toKill = current->next; - current->next = toKill->next; + while (current != NULL) { + if (current->killFlag == 1) { + toKill = current; + current = current->next; + + /* update link from previous item or from head */ + if (previous == NULL) { + self->head = current; + } else { + previous->next = current; + } + if (toKill->pKill != NULL) { toKill->pKill(toKill->pUserData); } free(toKill); } else { + previous = current; current = current->next; } }