0

I'm using Windows 7, compiling with gcc version 4.8.1. I have large, multi-threaded C program that makes a lot of calls to fopen and fclose. If it runs long enough, it starts failing to open files. I debugged using <errno.h> and am getting a Too many open files error, though there should never be more than a few files open at once. I've looked through the code many times and can't find the bug (for every call to fopen there's a corresponding call to fclose that gets executed, and no function uses more than one FILE* variable). Any further debugging suggestions? Particularly is there a function that returns the number of currently open files?

Madhav Datt
  • 1,065
  • 2
  • 10
  • 25
Nathan Schmidt
  • 374
  • 1
  • 4
  • 16
  • 1
    Making an `fprintf()` call to `stderr` with the filename or other identifying information each time you open and close a file is an obvious starting strategy, so you can see which ones are getting opened but not closed. – Crowman Jan 31 '16 at 16:29
  • 1
    May be, your threads don't close the files fast enough or you have too many threads. If it's on Linux, you can use `/proc/sys/fs/file-nr` to find open fds. Posting relevant info such as total threads, whether files are closed before creating more threads etc would help. Or perhaps, a [MCvE]. – P.P Jan 31 '16 at 16:30
  • The bug is that you are using too many files at once. Perhaps a rethink will not go amiss – Ed Heal Jan 31 '16 at 16:36
  • There are only 4 threads, and only a few files should be open at once. – Nathan Schmidt Jan 31 '16 at 16:37
  • all the calls to fopen are used with "r" mode, so I assumed there are no thread safety issues. Is this correct? – Nathan Schmidt Jan 31 '16 at 16:44
  • When you hit the error make the process `sleep()` and look with `openfiles` which files it still has opened. Maybe that gives you a hint where your file descriptor leak is located – Ctx Jan 31 '16 at 17:07
  • "only a few files should be open at once". Keyword: *should*, but the error indicates otherwise. Try @PaulGriffiths suggestion to track whether your fopen/fclose pairs all match up – Mark Tolonen Jan 31 '16 at 18:23
  • @PaulGriffiths suggestion should help – 4pie0 Jan 31 '16 at 18:24

1 Answers1

0

This is a "classic" resource leak issue. Most likely there is a code path where a file gets opened but not closed. Error handling code paths are among the usual suspects. You could search the web for "debug resource leaks c" or similar.

It will be easier to find info on tools for tracking memory resource leaks (I just googled). You could use a memory resource leak tool to solve this problem by allocating a small piece of memory every time you open a file and then freeing the memory when you close the file. If the memory leak tool is a reasonably good tool if will give you a stack trace or source file/line number for any leaked memory. That location will be right next to your file open.

Χpẘ
  • 3,403
  • 1
  • 13
  • 22