Why do I have a memory leak when I call malloc() to create a node for a linked list?

0

I'm a student and I'm currently working on a graphic project where I read a file containing numbers where their position in the file define the coordinates and the value define the altitude. For example :

1 0 0 -1 -1 0 1 1 0 0

-1 0 0 0 1 0 0 0 0 0

-1 1 0 0 -1 1 0 0 0 1

when x = 0, y = 0 then z = 1

when x = 1, y = 0 then z = 0

etc.

I wrote the code and it works fine. What I do is reading the file line by line and storing the line in a char** pointer called tab where each value *tab contains each value of a single line. Instead of creating 2d array, I stored each line in a linked list. The reason why I did that is because I already had a code that reads a file line by line and a code that split a string returning char **.

But I got some memory leaks. To find these leaks, I compile all the .c files with gcc using -g flag and then I use LLDB and Leaks command on OSX. I debug the code by executing the code step by step. Here's what I got from LLDB :

(lldb) n
Process 20739 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100001d66 fdf`ft_new(tab=0x0000000100207530) at ft_lst_gnl.c:22
   19       node = NULL;
-> 20       if (!(node = (t_gnl *)malloc(sizeof(t_gnl))))
   21           return (NULL);
   22       node->tab = tab;
   23       node->next = NULL;
   24       return (node);
   25   }

Before executing the line 20, I have no leak :

Date/Time:       2019-04-05 17:13:34.357 +0200
Launch Time:     2019-04-05 17:13:15.974 +0200
OS Version:      Mac OS X 10.12.6 (16G29)
Report Version:  7
Analysis Tool:   /Applications/Xcode.app/Contents/Developer/usr/bin/leaks
Analysis Tool Version:  Xcode 9.2 (9C40b)
----

leaks Report Version:  2.0
Process 24286: 607 nodes malloced for 66 KB
Process 24286: 0 leaks for 0 total leaked bytes.

But the moment I execute this line, the output of Leaks command changes :

leaks Report Version:  2.0
Process 24286: 608 nodes malloced for 66 KB
Process 24286: 1 leak for 16 total leaked bytes.
Leak: 0x100400100  size=16  zone: DefaultMallocZone_0x1000b2000
    0x00000000 0x00000000 0x00000000 0x00000000     ................

Here is my structure t_gnl :

typedef struct  s_gnl
{
    char            **tab;
    struct s_gnl    *next;
}               t_gnl;

I did a comparison with a test code where I used malloc to create another linked list or a string and I tested it with my debug method (LLDB + Leaks command) and the lines containing malloc() did not leak.

Thanks for reading.

EDIT: Here's a bit of my code. I can add more but I'm afraid that would be too much and confusing.

char **ft_strsplit(char const *s, char c);

t_gnl   *ft_store(char *file, t_gnl **list, t_env **fdf)
{
    int     fd;
    char    *line;
    int     height;
    int     ret;

    fd = open(file, O_RDONLY);
    if (fd < 0)
        return (NULL);
    height = 0;
    while ((ret = get_next_line(fd, &line)))
    {
        if (ret == -1)
            break ;
        ft_append(list, ft_strsplit(line, ' '));
        height++;
        ft_strdel(&line);
    }
    if (line)
        ft_strdel(&line);
    if (ret == -1 || *list == NULL)
    {
        close(fd);
        return (NULL);
    }
    *fdf = ft_init_env(*list, height);
    close(fd);
    return (*list);
}

static t_gnl    *ft_new(char **tab)
{
    t_gnl   *node;

    //node = NULL;
    if (!(node = (t_gnl *)malloc(sizeof(t_gnl))))
        return (NULL);
    node->tab = tab;
    node->next = NULL;
    return (node);
}

void            ft_append(t_gnl **head, char **tab)
{
    t_gnl   *list;
    t_gnl   *elem;

    elem = NULL;
    elem = ft_new(tab);
    if (*head != NULL)
    {
        list = *head;
        while (list->next)
            list = list->next;
        list->next = elem;
    }
    else
        *head = elem;
}

int main(int ac, char **av)
{
    t_gnl   *list;
    t_env   *fdf;

    list = NULL;
    fdf = NULL;
    list = ft_store(av[1], &list, &fdf);
    if (fdf == NULL || ft_error(&list, fdf->map->width))
    {
        ft_putstr("Error\n");
        exit(0);
    }
    ft_fdf(list, fdf);
}

PS: Sorry if I made some grammar mistakes, I'm french. If something isn't clear enough, I can try to give a better explanation. Also, it's my first post on Stackoverflow so I might forgot some information.

c
memory-leaks
lldb
asked on Stack Overflow Apr 5, 2019 by Daniel Coloma • edited Apr 5, 2019 by Daniel Coloma

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0