Valgrind消息:syscall中的无效文件描述符1024()

时间:2014-03-09 22:02:10

标签: c profiling valgrind

我在用valgrind测试我的bin时得到了这条消息:

==8454== Warning: invalid file descriptor 1024 in syscall close()

我打开一次然后关闭一次。下面的函数打开()一个文件,获取内容,然后关闭()文件。

int             parse(t_struct *data)
{
  int           fd;
  char          *s;

  if ((fd = open(data->file, O_RDONLY)) == -1)
    {
      fprintf(stderr, "Cannot open\n");
      return (-1);
    }
  while ((s = gnl(fd)) != NULL)
    {
      if (list_add_elem_at_back(&data->list, s) == FALSE)
        {
          fprintf(stderr,"Cannot stock\n");
          return (-1);
        }
    }
  close(fd);
  return (0);
}

哪里/有什么问题?

2 个答案:

答案 0 :(得分:1)

 if (list_add_elem_at_back(&data->list, s) == FALSE)
    {
      fprintf(stderr,"Cannot stock\n");
      return (-1);
    }

我不知道这是否是导致问题的原因,但这是一个错误:如果list_add_elem_at_back()失败,则退出parse()而不关闭文件。

答案 1 :(得分:0)

使用ulimit -n,它会显示最大的打开文件,我想你不会关闭fd,在某些代码的安静中。请跟踪每个fopen的代码,socketselect必须close