繁体   English   中英

Memory 泄漏在 C 与 static 变量,我不知道如何修复它

[英]Memory leak in C with static variable and i dont know how to fix it

作为练习,我正在编写 C function一次一行地从文件描述符中读取内容。 在练习中,我只允许使用标准库中的 read()、malloc() 和 free(),同时使用一个 static 变量。 function 有效,但当我到达我似乎无法解决的文件中的最后一行时,我不断收到持续的 memory 泄漏。

泄露:

Direct leak of 1 byte(s) in 1 object(s) allocated from:
    #0 0x7f37f253a4bf in __interceptor_malloc (/usr/lib/gcc/x86_64-pc-linux-gnu/12/libasan.so.8+0xbd4bf)
    #1 0x5644d5df4735 in offset /home/practical/Documents/Code/42/get_next_line/get_next_line.c:71
    #2 0x5644d5df49ba in get_next_line /home/practical/Documents/Code/42/get_next_line/get_next_line.c:116
    #3 0x5644d5df4a8b in main /home/practical/Documents/Code/42/get_next_line/get_next_line.c:138
    #4 0x7f37f22cd189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: 1 byte(s) leaked in 1 allocation(s).

代码:

#include "get_next_line.h"
#include <stdlib.h>
#include <unistd.h>
// needed for main:
#include <stdio.h>
#include <fcntl.h>

// finds the first line ending in \n from buffer
// this is the line for get_next_line to return
char    *next_line(char *a)
{
    int     i;
    char    *buf;

    if (!a || !a[0])
        return (NULL);
    i = 0;
    while (a[i] && a[i] != '\n')
        i++;
    if (a[i] == '\n')
        i++;
    buf = (char *)malloc((sizeof(char) * i) + 1);
    if (!buf)
        return (NULL);
    i = 0;
    while (a[i] && a[i] != '\n')
    {
        buf[i] = a[i];
        i++;
    }
    if (a[i] == '\n')
        buf[i++] = '\n';
    buf[i] = '\0';
    return (buf);
}

// finds the end of the line we just returned in the buffer
// moves it to the end of new buffer
// changes the \n before it to '\0', so our new buffer does not contain it
// this offsets our buffer, the buffer now starts
// at the next line to be printed
char    *offset(char *a)
{
    char    *buf;
    int     i;
    int     x;

    i = 0;
    x = 0;
    while (a[i] && a[i] != '\n')
        i++;
    if (a[i] == '\0')
    {
        free(a);
        return (NULL);
    }
    if (a[i] == '\n')
        i++;
    buf = (char *)malloc(ft_strlen(a) - i + 1);
    if (!buf)
        return (NULL);
    while (a[i + x])
    {
        buf[x] = a[i + x];
        x++;
    }
    buf[x] = '\0';
    free(a);
    return (buf);
}

// static buffer str starts at the next line to be printed
// we use strchr to find if there is a \n in the buffer
// if theres not, we read from the file into the buffer
// if \n is present, this is the line to print
// next_line then gets the first line ending in '\n' from the static buffer
// we return this line
// then we offset the static buffer by the line we just returned
char    *get_next_line(int fd)
{
    static char     *str;
    char            *buf;
    int             i;

    if (fd < 0 || BUFFER_SIZE <= 0)
        return (NULL);
    buf = (char *)malloc((sizeof(char) * BUFFER_SIZE) + 1);
    if (!buf)
        return (NULL);
    i = 1;
    while (!(ft_strchr(str, '\n')) && i != 0)
    {
        i = read(fd, buf, BUFFER_SIZE);
        if (i == -1)
        {
            free(buf);
            return (NULL);
        }
        buf[i] = '\0';
        str = ft_strjoin(str, buf);
    }
    free(buf);
    buf = next_line(str);
    str = offset(str);
    return (buf);
}

int main(int argc, char *argv[])
{
    char    *line;
    int     fd;

    if (argc < 2)
    {
        printf("Usage: %s <file>\n", argv[0]);
        return (1);
    }
    fd = open(argv[1], O_RDONLY);
    if (fd == -1)
    {
        perror("Failed to open file");
        return (1);
    }
    while (line != NULL)
    {
        line = get_next_line(fd);
        printf("%s", line);
        free(line);
    }
    close (fd);
    return (0);
}

ft_strjoin

char    *ft_strdup(char *src)
{
    char    *dst;
    int     i;

    i = 0;
    dst = malloc(ft_strlen(src) + 1);
    if (!dst)
        return (NULL);
    while (src[i])
    {
        dst[i] = src[i];
        i++;
    }
    dst[i] = '\0';
    return (dst);
}

char    *ft_strcat(char *dest, char *src)
{
    int i;
    int l;

    i = 0;
    l = ft_strlen(dest);
    while (src[i] != '\0')
    {
        dest[i + l] = src[i];
        i++;
    }
    dest[i + l] = '\0';
    return (dest);
}

char    *ft_strjoin(char const *s1, char const *s2)
{
    char    *str;
    size_t  len;

    if (!s1 && !s2)
        return (ft_strdup(""));
    if (s1 && !s2)
        return (ft_strdup((char *)s1));
    if (!s1 && s2)
        return (ft_strdup((char *)s2));
    len = ft_strlen((char *)s1) + ft_strlen((char *)s2);
    str = malloc(sizeof(char) * (len + 1));
    if (!str)
        return (NULL);
    str[0] = '\0';
    ft_strcat(str, (char *)s1);
    ft_strcat(str, (char *)s2);
    return (str);
}

问题

我检查了代码,似乎我正在释放一切。 我认为问题可能出在get_next_line function 中返回buf ,它由next_line function 分配。我无法释放它,因为我正在返回它,但我稍后会在main()中释放字符串。

也许我应该在到达我正在从描述符中读取的文件末尾后释放 static 变量?

任何建议表示赞赏,谢谢

“似乎我正在释放所有东西”--> 代码在完成后不会释放get_next_line()中的str

暂无
暂无

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

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