File handle left behind by C++ code in Linux

I am trying to debug an issue in my code. I have a process A which runs continuously till I ask it to stop. Inside A I do the following: - mount partition /dev/sda1 - `open()` // creates an empty file X - `write()` // write some bytes to it - `close()` // close the file - `processFile()` // Perform some operation - `remove()` // remove file - umount /dev/sda1 Note I test after each operation above if it was successful or not befor moving forward. When I do `lsof | grep A` it shows the file handle of X being owned by process A. I also see it has a (deleted). This prevents me from umounting the partition. Why is this happening and how can I get around this issue? **EDIT**: Thanks all. Here is the snippet of the code: tarFileDesc = _pSysCall->open("test.tar", O_CREAT | O_RDWR | O_APPEND, 0777); if (0 > tarFileDesc) return false; ... some logging here // Write http stream to tar file, istr is an argument to my function int read_buffer_size = 0; buffer = new char[4096]; while (!istr.eof() && count < content_length) { if ((content_length - count) >= 4096) read_buffer_size = 4096; else read_buffer_size = content_length - count; memset(buffer, 0, 4096); istr.read(buffer, read_buffer_size); std::streamsize in_bytes = istr.gcount(); if (istr.fail() || istr.bad()) { status = false; break; } if (write(tarFileDesc, buffer, in_bytes) != in_bytes) { status = false; break; } count += in_bytes; } // Cleanup buffer delete[] buffer; if ((0 > tarFileDesc) && (0 != close(tarFileDesc))) return false; if (0 != system("tar C /test -xvf test.tar")) return false; if (0 != remove("test.tar")) return false; Note I even tried just doing the open, close and remove. But I still see the handle being held by process.
Please add code, not meta-code. Locking problems can't usually be solved by meta-code. Boil down the original code, then we can try to debug it.

以上就是File handle left behind by C++ code in Linux的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏