C
C
COUT_man2021-12-02 10:41:58
C++ / C#
COUT_man, 2021-12-02 10:41:58

How to deal with valgrind output?

Please tell me how to deal with the result of valgrind. I can't quite figure out where the problem is. At me or in the program. Although I'm more than sure that my code is to blame. But I have no idea where exactly I messed up...

By assignment, I have to describe the get_next_line function.
Made it an implementation. But when running with the help of the program for testing, I stumble upon a memory leak:

==890== 2 bytes in 1 blocks are definitely lost in loss record 1 of 1
==890==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==890==    by 0x407335: get_next_line (get_next_line.c:31)
==890==    by 0x402C86: gnl(int, char const*) (gnl.cpp:20)
==890==    by 0x405272: main (mandatory.cpp:41)


The string referenced by valgrind
31result = (char *)malloc(sizeof(char) * 2);
32    if (!result)
33    {
34       free (result);
35        return (0);
36    }


Of course, I understand that, in a good way, you need to free memory after using mallos, but the question of saving data arises. I tried to make a crutch in the form of passing a value and doing the cleanup with free ().
I drew something like:
getresult = result;
    free(result);
    return (getresult);

And received a new portion of slap in the face. Despite the fact that the tests themselves pass successfully.
==922== Invalid free() / delete / delete[] / realloc()
==922==    at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==922==    by 0x402CDB: gnl(int, char const*) (gnl.cpp:25)
==922==    by 0x4054AB: main (mandatory.cpp:46)
==922==  Address 0x4db6ce0 is 0 bytes inside a block of size 2 free'd
==922==    at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==922==    by 0x40745B: get_next_line (get_next_line.c:70)
==922==    by 0x402C86: gnl(int, char const*) (gnl.cpp:20)
==922==    by 0x4054AB: main (mandatory.cpp:46)
==922==  Block was alloc'd at
==922==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==922==    by 0x40755B: ft_strjoin (get_next_line_utils.c:40)
==922==    by 0x407431: get_next_line (get_next_line.c:65)
==922==    by 0x402C86: gnl(int, char const*) (gnl.cpp:20)
==922==    by 0x4054AB: main (mandatory.cpp:46)


the code itself:
char    *get_next_line(int fd)
{
    char *getresult;
    char    *result;
    char    *readline;
    char    symb;
    int readbyte;
    int i;
    int all_readbyte;
    char flag;

    all_readbyte = 0;
    if (!fd && BUFFER_SIZE <= 0)
        return (0);
    flag = 'n';
    readbyte = read(fd, &symb, 1);
    all_readbyte++;
    result = (char *)malloc(sizeof(char) * 2);
    if (!result)
    {
        free (result);
        return (0);
    }
    result[0] = '\0';
    result[1] = '\0';
    if (readbyte <= 0)
        return (0);
    while (flag != 'y')
    {
        i = 0;
        readline = (char *)malloc(sizeof(char) * BUFFER_SIZE + 1);
        if (!readline)
            return (0);
        while (i < BUFFER_SIZE)
        {
            readline[i++] = symb;
            if (symb == '\n' || symb == '\0' || readbyte <= 0 || !symb)
            {
                flag = 'y';
                break;
            }
            readbyte = read(fd, &symb, 1);
            if (readbyte <= 0)
            {
                flag = 'y';
                break;
            }
            all_readbyte++;
        }
        readline[i] = '\0';
        result = ft_strjoin(result, readline);
        free (readline);
        if (readbyte == 0)
        {
            getresult = result;
            free(result);
            return (getresult);
        }
    }
    if (all_readbyte == 1)
        result[0] = symb;
    getresult = result;
    free(result);
    return (getresult);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-12-02
@shurshur

When hitting a block: the
if (!result)
memory is NOT actually allocated and free from it is an attempt to free the memory at the NULL address. Of course, this won't work.
upd: I read the question again more carefully. We must not forget to do free in the final place of use of this result:

result = get_next_line();<br>
...do_something_with_result...<br>
free(result);

Otherwise, yes, each call to get_next_line will allocate a new block of 2 bytes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question