Question

I'm using Xcode5/clang on OSX 10.9.2. My program starts acting up on

std::ifstream file(path);
if (!file.is_open())
    throw std::runtime_error("somethings wrong");

after a couple hundred (or thousand?) opening and closing files. When that happens, I cannot open any file anymore, neither with fstream nor with fopen(), which makes me think I ran out of file descriptors. According to the documentation on ifstream, the file gets closed when the fstream object gets destroyed. All my fstream objects live on the stack, I never allocate any with new, so if my stack gets cleaned up properly, I don't have to call fstream::close() because when the object goes out of scope, the file is closed.

But apparently, I'm still leaking the file descriptors somewhere.

Questions: 1. Can I run out of file descriptors even if ifsteam objects are cleaned up as planned? Like, can file descriptors be re-used after they were closed, or are they gone forever once used and closed? 2. Is there a way I can possibly leak fstream objects, leaving them open, even if they all live on my stack?

Was it helpful?

Solution

Nevermind, I found my problem.

I was not leaking fstream objects at all, but I have been leaking sqlite handles like crazy. I found it through careful use of

lsof -p my_processes_pid | wc

lsof is a really helpful command!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top