Hello all,
I am at the final stages of a project, and am dealing with leaks.
I am using valgrind (newbie!!).
I am getting the following output:
Code:
==11236== HEAP SUMMARY:
==11236== in use at exit: 10 bytes in 2 blocks
==11236== total heap usage: 13 allocs, 11 frees, 1,080 bytes allocated
==11236==
==11236== Searching for pointers to 2 not-freed blocks
==11236== Checked 65,928 bytes
==11236==
==11236== 10 bytes in 2 blocks are definitely lost in loss record 1 of 1
==11236== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11236== by 0x400DD0: ft_strnew (in /home/ubuntu-mate/Desktop/42/get_next_line/a.out)
==11236== by 0x400B17: ft_trim_line (in /home/ubuntu-mate/Desktop/42/get_next_line/a.out)
==11236== by 0x400D1B: get_next_line (in /home/ubuntu-mate/Desktop/42/get_next_line/a.out)
==11236== by 0x4008F3: main (in /home/ubuntu-mate/Desktop/42/get_next_line/a.out)
==11236==
==11236== LEAK SUMMARY:
==11236== definitely lost: 10 bytes in 2 blocks
==11236== indirectly lost: 0 bytes in 0 blocks
==11236== possibly lost: 0 bytes in 0 blocks
==11236== still reachable: 0 bytes in 0 blocks
==11236== suppressed: 0 bytes in 0 blocks
==11236==
==11236== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==11236== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
My code looks like this:
Code:
char *ft_strcomb(char *s1, char *s2)
{
char *fresh;
size_t i;
size_t j;
fresh = ft_strnew(ft_strlen(s1) + ft_strlen(s2));
if (fresh == NULL)
return (NULL);
j = 0;
i = 0;
while (s1[i])
fresh[j++] = s1[i++];
i = 0;
// free(s1);
while (s2[i])
fresh[j++] = s2[i++];
return (fresh);
}
static int ft_check_line(const char *str)
{
int i;
i = 0;
while (str && str[i] != '\0')
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}
static char *ft_trim_line(char *str)
{
char *ret;
int i;
i = 0;
while (str[i] != '\n' && str[i] != '\0')
i++;
ret = ft_strnew(i);
i = 0;
while (str[i] != '\n' && str[i] != '\0')
{
ret[i] = str[i];
i++;
}
return (ret);
}
static char *ft_get_remainder(char *str)
{
int i;
char *ret;
i = 0;
if (str[i] == '\0')
return (NULL);
while (str[i] != '\n' && str[i] != '\0')
i++;
ret = ft_strsub(str, i + 1, ft_strlen(str) - i);
free(str);
return (ret);
}
int get_next_line(int const fd, char **line)
{
char buf[BUFF_SIZE + 1];
static char *str;
int ret;
char *ptr;
//char *otp;
if (read(fd, buf, 0) < 0)
return (-1);
if (BUFF_SIZE < 1 || !line || (fd < 0))
return (-1);
if (!str)
{
str = ft_strnew(0);
if (str == NULL)
return (-1);
}
while (!(ft_check_line((const char*)str)) && (ret = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[ret] = '\0';
ptr = str;
str = ft_strcomb(ptr, buf);
free(ptr);
}
*line = ft_trim_line(str);
//*line = (otp = ft_trim_line(str));
//free(otp);
ptr = str;
if ((str = ft_get_remainder(ptr)) == NULL)
{free(ptr);
return (0);
}
return (1);
}
And ft_strnew function:
Code:
char *ft_strnew(size_t size)
{
size_t i;
char *str;
i = 0;
str = (char*)malloc(sizeof(*str) * (size + 1));
if (!str)
return (0);
while (i < size)
{
str[i] = '\0';
i++;
}
str[i] = '\0';
return (str);
}
If I'm not mistaken, the leak comes from the string that is returned from ft_trim_llne as it is never freed.
When I try replacing:
Code:
*line = ft_trim_line(str);
with:
Code:
*line = (otp = ft_trim_line(str));
free(otp);
I no longer have leaks, but instead have an error that I have "34 errors from 7 contexts".
Any idea what the problem might be??