Free problems in C language : I'm working on a project in C language. The purpose of the project is to code a function that returns the next line of a file every time it's called.
I'm only allowed to use the malloc, free and read functions.
Here's the errors I'm getting :
[Errors][1]


  [1]: http://i.stack.imgur.com/VYh0l.jpg
And here's my code :

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include "get_next_line.h"

    int		my_putstr(char *str)
    {
      int		i;

      if (str == NULL)
        return (1);
      i = 0;
      while (str[i] != '\0')
        {
          write(1, &str[i], 1);
          i = i + 1;
        }
      return (0);
    }
    
    char		*my_strcpy(char *src, char *dest)
    {
      int		i;
      int		r;
    
      r = 0;
      i = 0;
      if (src == NULL)
        return (dest);
      while (dest != NULL && dest[i] != '\0')
        i = i + 1;
      while (src[r] != '\0')
        {
          dest[i] = src[r];
          i = i + 1;
          r = r + 1;
        }
      dest[i] = '\0';
      return (dest);
    }
    
    int		check_back_n(t_stock *stock)
    {
      int		i;
    
      if (stock->save == NULL)
        return (1);
      i = 0;
      while (stock->save[i] != '\0')
        {
          if (stock->save[i] == '\n')
            return (0);
          i = i + 1;
        }
      return (1);
    }
    
    int		my_realloc(t_stock *stock)
    {
      if ((stock->temp = malloc(sizeof(char) *
	    		    (READ_SIZE * stock->counter) + 1)) == NULL)
        return (1);
      stock->temp = my_strcpy(stock->save, stock->temp);
      free(stock->save);
      if ((stock->save = malloc(sizeof(char) *
	    	       (READ_SIZE * (stock->counter + 1)) + 1)) == NULL)
        return (1);
      stock->save = my_strcpy(stock->temp, stock->save);
      free(stock->temp);
      return (0);
    }

    char		*fill_over(t_stock *stock, char *over)
    {
      stock->r = 0;
      stock->i = 0;
      while (stock->save[stock->i] != '\n')
        stock->i = stock->i + 1;
      stock->save[stock->i] = '\0';
      stock->i = stock->i + 1;
      if ((over = malloc(sizeof(char) * READ_SIZE) + 1) == NULL)
        return (NULL);
      while (stock->save[stock->i] != '\0')
        {
          stock->save[stock->i] = over[stock->r];
          stock->save[stock->i] = '\0';
          stock->i = stock->i + 1;
          stock->r = stock->r + 1;
        }
      return (over);
    }
    
    char		*get_next_line(const int fd)
    {
      t_stock	stock;
      static char	*over;
    
      stock.counter = 2;
      if ((stock.save = malloc(sizeof(char) * READ_SIZE) + 1) == NULL)
        return (NULL);
      stock.save = my_strcpy(over, stock.save);
      free(over);
      while (check_back_n(&stock) == 1)
        {
          if (my_realloc(&stock) == 1)
	    return (NULL);
          if ((stock.buffer = malloc(sizeof(char) * READ_SIZE) + 1) == NULL)
	    return (NULL);
          if ((stock.read_return = read(fd, stock.buffer, READ_SIZE)) == -1||
	  (stock.read_return == 0))
	    return (stock.save);
          stock.counter = stock.counter + 1;
          stock.save = my_strcpy(stock.buffer, stock.save);
          free(stock.buffer);
        }
      if ((over = fill_over(&stock, over)) == NULL)
        return (NULL);
      return (stock.save);
    }

    int		main()
    {
      char		*s;
      int		fd;
    
      fd = open("test", O_RDONLY);
      while (s = get_next_line(fd))
        {
          my_putstr(s);
          write(1, "\n", 1);
          free(s);
        }
      return (0);
    }

