中止陷阱 6:释放未分配的指针

Abort trap 6: Freeing unallocated pointer

这是销毁循环链表的代码。当它运行时,它会产生一个中止陷阱 6: pointer being freed was not allocated

NODE *roamer = list->head;
        do{
            NODE *oldRoamer = roamer;
            roamer = roamer->next;
            printf("freeing prev");
            free(oldRoamer);
            printf("prev freed");
        }while (roamer != NULL);

程序多次打印两个 printf 语句,但在打印 "freeing prev".

后立即给出错误

它是圆形的。这意味着最后一个项目指向第一个,它已经被释放了。

do
    ....
while (roamer != list->head);

用于停止循环的条件不正确。

NODE *roamer = list->head;
do {
   NODE *oldRoamer = roamer;
   roamer = roamer->next;
   printf("freeing prev");
   free(oldRoamer);
   printf("prev freed");
} while (roamer != list->head);
// Stop when roamer reaches back to head