// main.cpp // // sample code to demonstrate the leak of file handles when nftw() is used // together with the FTW_CHDIR flag. // #include #include #include #include using namespace std; #include // #include #include #ifndef MY_FLAGS # define MY_FLAGS 0 #endif int my_callback(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) { // cout << "file path=" << fpath << endl; return 0; /* 0 to tell nftw() to continue */ } int main(int argc, char *argv[]) { // int flags=0; int flags=MY_FLAGS; for (int i=0; i<100; i++) { // cout << std::endl << "loop #" << i << std::endl; // note: it doesn't make a difference if "." or another // existing directory is used as the 1st argument nftw("." , my_callback, 20, flags); } // now lets print a list of open filehandles ostringstream lsof_cmd; lsof_cmd << "lsof -p " << getpid(); system(lsof_cmd.str().c_str()); return 0; }