简体   繁体   English

C内存泄漏和Valgrind输出

[英]C Memory leaks and Valgrind output

I am doing some learning with C, and am having trouble identifying a memory leak situation. 我正在用C学习,并且无法识别内存泄漏情况。

First, some code: 首先,一些代码:


My main function: 我的主要功能:

#define FILE_NAME "../data/input.txt"

char * testGetLine( FILE * );
int testGetCount(void);

int main(void)
{
    int count = 0;
    FILE * fptr;

    if ((fptr = fopen(FILE_NAME, "r")) != NULL) {
        char * line;
        while ((line = testGetLine(fptr)) != NULL) {            

            printf("%s", line);
            free(line); count++;
        }

        free(line); count++;

    } else {
        printf("%s\n", "Could not read file...");
    }

    // testing statements
    printf("testGetLine was called %d times\n", testGetCount());
    printf("free(line) was called %d times\n", count);

    fclose(fptr);
    return 0;
}

and my getline function: 和我的getline函数:

#define LINE_BUFFER 500

int count = 0;

char * testGetLine(FILE * fptr)
{
    extern int count;

    char * line;
    line = malloc(sizeof(char) * LINE_BUFFER);
    count++;

    return fgets(line, LINE_BUFFER, fptr);
}

int testGetCount(void) {
    extern int count;
    return count;
}

my understanding is that I would need to call free everytime I have called my testGetLine function, which I do. 我的理解是,每当我调用testGetLine函数时,我都需要free调用,我这样做。 By my count, on a simple text file with four lines I need to call free 5 times. 根据我的统计,在一个有四行的简单文本文件中,我需要免费拨打5次。 I verify that with my testing statements in the following output: 我在以下输出中使用我的测试语句验证:

This is in line 01
Now I am in line 02
line 03 here
and we finish with line 04
testGetLine was called 5 times
free(line) was called 5 times

What I am having trouble with is, valgrind says that I alloc 6 times, and am only calling free 5 times. 我遇到麻烦的是,valgrind说我alloc 6次,而且我只是free拨打5次。 Here is truncated output from valgrind: 这是valgrind的截断输出:

HEAP SUMMARY:
    in use at exit: 500 bytes in 1 blocks
  total heap usage: 6 allocs, 5 frees, 3,068 bytes allocated
500 bytes in 1 blocks are definitely lost in loss record 1 of 1
   at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x4007A5: testGetLine (testGetLine.c:13)
   by 0x400728: main (tester.c:16)
LEAK SUMMARY:
   definitely lost: 500 bytes in 1 blocks
   indirectly lost: 0 bytes in 0 blocks
     possibly lost: 0 bytes in 0 blocks
   still reachable: 0 bytes in 0 blocks
        suppressed: 0 bytes in 0 blocks

I feel I am missing something with the memory management. 我觉得我错过了内存管理的一些东西。 Where is the 6th memory allocation that valgrind says I am using? valgrind说我正在使用的第6个内存分配在哪里? and how should I free it? 我应该如何释放它?


Followup to implement Adrian's answer 跟进实施阿德里安的答案

testGetLine adjustment: testGetLine调整:

char * testGetLine(FILE * fptr)
{
    extern int count;

    char * line;
    line = malloc(sizeof(char) * LINE_BUFFER);
    count++;

    if (fgets(line, LINE_BUFFER, fptr) == NULL) {
        line[0] = '\0';
    }

    return line;
}

main while loop adjustment: main循环调整:

while ((line = testGetLine(fptr))[0] != '\0') {            

    printf("%s", line);
    free(line); count++;
}

free(line); count++;

fgets return description: fgets返回描述:

On success, the function returns str. 成功时,函数返回str。 If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). 如果在尝试读取字符时遇到文件结尾,则设置eof指示符(feof)。 If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). 如果在读取任何字符之前发生这种情况,则返回的指针是空指针(并且str的内容保持不变)。 If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed). 如果发生读取错误,则设置错误指示符(ferror)并返回空指针(但str指向的内容可能已更改)。

When fgets doesn't read anything it doesn't return the char * that you used malloc on. fgets没有读取任何内容时,它不会返回您使用malloc的char *

Therefore, the malloc in your last call isn't being freed. 因此,您上次调用中的malloc未被释放。 The statement after your while doesn't work as you want. 一段时间后的声明无法正常工作。

Solution: change your return and return line instead: 解决方案:改变您的退货和退货line

char * testGetLine(FILE * fptr)
{
    extern int count;

    char * line;
    line = malloc(sizeof(char) * LINE_BUFFER);
    count++;
    fgets(line, LINE_BUFFER, fptr);
    return line;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM