Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QProcess::setStandardOutputFile file locked after QProcess finished
I am running a command that I record to a file and parse the output. The file is temporary, so after I am done I would like to delete it. I have a QFile object that I give the path to the file, then use its QFile::filename() function to pass to QProcess::setStandardOutputFile(). After all of my parsing is done, I try to delete it with QFile::remove() (after checking it exists still). I am calling QProcess::close() before trying and the remove() function returns false. I can't even delete the file in windows after I close my program. Is there something I need to do in my QProcess object to close the output file after the QProcess object ends?
Thanks for your time!
@MNorvalls Did you make sure the process really terminated? It sounds like the process is still running and thus keeping the file open.
I am calling QProcess::close()
I agree with @jsulm's hunch that the sub-process is still running. If you temporarily try
QProcess::kill()instead, does that then mean you can delete the file?
Hello, and thanks for the replies guys. I have added a kill() call after close() and I still have a file lock issue. Windows reports it is being locked by Java.
So let's start slowly. Your Qt program does not use the Java Platform. So to me that implies you have some other program accessing it, keeping it locked, which has nothing to do with your Qt program?
You are redirecting sub-process output into that file. I don't know what command is/does. If the sub-process you run is something innocent, like say
cmd /c echo hello, are you then able to delete the file at conclusion of the command?
This post is deleted!
Apologies. Yes you are correct. I believe the QProcess exe I am running does use Java. I was under the assumption that the QProcess object controlled the file I/O, so I assumed by worst case destroying the object, it would solve the issue.
I also tried to reset the output file to QProcess::setStandardOutputFile(""); no dice as well.
I believe this Java runtime is a child process of my QProcess, and without digging through the windows API weeds to track down the PID and close the child process, it looks like I will just have to leave the runtime to terminate itself and not delete the temp file
I think this is right. If the other process has that file open, and if even you going
QProcess::kill()does not seem to release it from what you said earlier, I don't think there is more you can do.
One thing you might want to try is using QTemporaryFile. I suspect it will have the same issue being unable to delete, but it might behave differently I suppose.
Yeah I thought about that, but I assume the lock will still be held and the temp file will just fail to be deleted still. This way at least I know where the file is on each program start and can delete or overwrite it if needed.
Thanks for your help though!