释放 getline() 缓冲区后丢失的字节数

bytes lost after freeing getline() buffer

我四处寻找有关使用 getline() 时 valgrind 内存泄漏的帖子。

基本上我的代码所做的是使用 getline() 在一行中读取。

int main(){
LL A;
LLinit(&A);
size_t len = 5;
char *lineOrigin = (char *)malloc(len);
char *line_ptr = lineOrigin;
getline(&line_ptr,&len,stdin);
int status, val;
while( (status = sscanf(line_ptr,"%d",&val) ) >= 0){
    printf("%d\n",status);
    //if(isBadInput(line_ptr)){...}
    if(status == 0){
        printf("ENCOUNTERED BAD INPUT. STOPPING\n");
        return 1;
    }
    Node *node_ptr = (Node *)malloc(sizeof(Node));
    node_ptr->val = val;
    append(&A,node_ptr);
    line_ptr = lookPastSpace(line_ptr);
}
printLL(&A);
freeLL(&A);
free(lineOrigin);
return 0;

}

如您所见,我为 char *lineOrigin 分配了一些内存,然后有第二个指针 *line_ptr,我指向 *lineOrigin,我稍后在代码中对其进行变异。

这段代码几乎从 stdin 中读取整数并将它们按顺序存储到 LinkedList 中。

每次 sscanf 将一个 int 读入 &val 时,我使用 lookPastSpace() 将 *line_ptr 移动到字符串中的非 space 字符上,直到遇到另一个 space,然后我将指针再移动一个档位,这样 line_ptr 现在指向字符串中可以读取新 int 的下一个位置

line_ptr = "123 456 789\n"
line_ptr = lookPastSpace(line_ptr)
line_ptr = "456 789\n"
...
line_ptr = "" -->sscanf stops.

之后,我沿着链表遍历并释放每个分配的节点,并且由于节点没有分配内存的内容,这就是我必须去释放 LL 的内存。

然后,稍后,我释放了 char *lineOrigin,这应该可以工作,因为它是最初分配的缓冲区,但它没有。

    tylerjgabb@lectura:~/HW6/medianDir$ make mem
valgrind median
==11827== Memcheck, a memory error detector
==11827== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11827== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==11827== Command: median
==11827==
123 456 789
1
1
1
[123, 456, 789]
==11827== Invalid free() / delete / delete[] / realloc()
==11827==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11827==    by 0x400B37: main (in /p3/ht/tylerjgabb/HW6/medianDir/median)
==11827==  Address 0x51ff040 is 0 bytes inside a block of size 5 free'd
==11827==    at 0x4C2CE8E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11827==    by 0x4EA5F9A: getdelim (iogetdelim.c:106)
==11827==    by 0x400A73: main (in /p3/ht/tylerjgabb/HW6/medianDir/median)
==11827==
==11827==
==11827== HEAP SUMMARY:
==11827==     in use at exit: 13 bytes in 1 blocks
==11827==   total heap usage: 5 allocs, 5 frees, 66 bytes allocated
==11827==
==11827== LEAK SUMMARY:
==11827==    definitely lost: 13 bytes in 1 blocks
==11827==    indirectly lost: 0 bytes in 0 blocks
==11827==      possibly lost: 0 bytes in 0 blocks
==11827==    still reachable: 0 bytes in 0 blocks
==11827==         suppressed: 0 bytes in 0 blocks
==11827== Rerun with --leak-check=full to see details of leaked memory
==11827==
==11827== For counts of detected and suppressed errors, rerun with: -v
==11827== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
tylerjgabb@lectura:~/HW6/medianDir$

我有点卡住了,也很困惑。有什么想法吗?

----------------关于 TYPEDEFS 和 LL 函数的说明---------------- LLfuncs.c

    #include "LinkedListHeader.h"
    #include <stdlib.h>

    void LLinit(LL *LL_ptr){
        LL_ptr->head_ptr = NULL;
        LL_ptr->tail_ptr = NULL;
        LL_ptr->length = 0;
    }


void freeLL(LL *LL_ptr){
    Node *curr_ptr = LL_ptr->head_ptr;
    while(curr_ptr != NULL){
        Node *next_ptr = curr_ptr->next_ptr;
        free(curr_ptr);
        curr_ptr = next_ptr;
    }
}

以上只是我的一小部分LLfuncs.c

LinkedListHeader.h

/* This is a header file containing typedefs and macros for linked lists *Tyler J Gabb
 *For assignment 6a. Shuffle.
 */
#include <stdio.h>
#define showLocs(LL) printf("head_ptr = %p, tail_ptr = %p length = %d\n",LL.head_ptr,LL.tail_ptr,LL.length)


typedef struct LinkedListNode {
  int val;
  struct LinkedListNode *next_ptr;
} Node;

typedef struct LinkedList {
    Node *head_ptr;
    Node *tail_ptr;
    int length;
} LL;

LLinit() 初始化任何最近声明的 LL 类型,使其头部和尾部位于 0x0,长度为 0。这有助于其他函数改变 LL

------------------------一些额外的有趣信息-------- ----

如果输入字符串的大小小于 len 的原始值,那么我不会发生内存泄漏(有时)

您基本上具有以下模式:

char *lineOrigin, *line_ptr;
size_t len = 5;

lineOrigin = malloc(len);
line_ptr = lineOrigin;
getline(&line_ptr, &len, stdin);

因为 getline() 会在必要时重新分配缓冲区,在 getline() 调用之后,lineOrigin 可能不再有效。

本质上,您在 lineOrigin 中保留了一个旧指针的副本,该副本可能会被 getline() 调用释放。您没有释放可能由 getline()line_ptr 重新分配的当前缓冲区,而是错误地(使用)和 free() 一些旧值不再有效:lineOrigin.


图案的正确写法很简单。您首先将行指针定义为 NULL 并将其分配的大小设为零。我将分配的大小称为 line_max,其中 line_len 反映了当前行的长度,但显然您可以使用您认为最易于维护的任何变量命名。

char   *line_ptr = NULL;
size_t  line_max = 0;
ssize_t line_len;

您使用

阅读了一行或多行
line_len = getline(&line_ptr, &line_max, stdin);
if (line_len > 0) {
    /* You have a line in line_ptr */
} else
if (ferror(stdin)) {
    /* Error reading from standard input (rare!) */

} else
if (feof(stdin)) {
    /* No more data to read from standard input. */
}

您可以根据需要多次重复上述操作,但您需要意识到 line_ptrline_max 的值可能会因一次调用而异。

完成后,释放行缓冲区。因为 free(NULL); 总是安全的,所以即使没有读取实际行也是安全的:

free(line_ptr);
line_ptr = NULL;
line_max = 0;

现在,清除变量并不是绝对必要的,但我发现这是一个很好的做法,有时甚至可以帮助调试。

另请注意,即使在 getline() 调用之前释放缓冲区并清除变量也是完全可以的,因为 getline() 不关心它是否得到一个零的 NULL 指针大小,或先前分配的非零大小的缓冲区。