链接列表剪切导致内存泄漏

时间:2019-02-17 14:30:31

标签: c linked-list nodes

我已经为这个问题苦苦挣扎了好几个小时,并在网上进行了很多搜索,但是找不到解决方法。

我创建了一个链表结构,该链表的结构节点为head。 现在,我想在特定的给定节点上拆分链接列表,返回第二个列表。

代码:

struct list* list_cut_after(struct list* l, struct node* n) {

      if (l && n) {

          if (n->next == NULL){
                struct list *l2 = list_init();
                return l2;
            }

               struct list *l2 = list_init();
               l2->head = n->next;
               n->next = NULL;



            }
            else {
                return NULL;
            }
        }

此代码似乎对我有用,它使给定节点旁边的节点成为列表2的头部。但这对我而言不起作用,测试时我有内存泄漏:

测试代码(所有其他功能均正常工作):

START_TEST (test_cut)
{
    struct list* l = list_init();
    struct node* n;

    for (int i = 1; i < 6; i++) { 
        n = list_new_node(i);
        list_add_back(l, n);
    }



    struct node *y = list_get_ith(l, 2);

    list_cut_after(l, y);



    list_cleanup(l);

}
END_TEST

内存泄漏错误:

==21284==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f6a6f792b50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x55de71ac1b01 in list_init /home/bart/Downloads/insertion_sort/list.c:36
    #2 0x55de71ac20a7 in list_cut_after /home/bart/Downloads/insertion_sort/list.c:345
    #3 0x55de71ac16f6 in test_cut /home/bart/Downloads/insertion_sort/check_list.c:395
    #4 0x55de71ac4bb5 in srunner_run (/home/bart/Downloads/insertion_sort/check_list+0x6bb5)

Indirect leak of 48 byte(s) in 3 object(s) allocated from:
    #0 0x7f6a6f792b50 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb50)
    #1 0x55de71ac1b39 in list_new_node /home/bart/Downloads/insertion_sort/list.c:51
    #2 0x55de71ac16ad in test_cut /home/bart/Downloads/insertion_sort/check_list.c:387
    #3 0x55de71ac4bb5 in srunner_run (/home/bart/Downloads/insertion_sort/check_list+0x6bb5)

SUMMARY: AddressSanitizer: 56 byte(s) leaked in 4 allocation(s).

清理代码:

int list_unlink_node(struct list* l, struct node* n) {

        if (n != NULL || l != NULL) {
            if (n == l->head){
                l->head = list_next(n);
                n->next = NULL;
                return 0;
            }
            else {
                list_prev(l, n)->next = list_next(n);
                n->next = NULL;
                return 0;
            }

        }
        else {
            return 1;
        }

    }

    void list_free_node(struct node* n) {
        free(n);

    }
        int list_cleanup(struct list* l) {
            if (l){

                struct node *current = list_head(l);
                struct node *next;

                while (current != NULL)
                {
                    next = list_next(current);
                    list_unlink_node(l, current);
                    list_free_node(current);
                    current = next;

                }
                free(l);
                return 0;
            }
            else {
                return 1;
            }


        }

1 个答案:

答案 0 :(得分:0)

list_cut_after返回指向新列表的指针,但是您没有保存它。这意味着内存丢失,并且您有泄漏。

保存此函数的返回值,然后稍后清理返回的列表。

struct list *l2 = list_cut_after(l, y);

list_cleanup(l);
list_cleanup(l2);