QProcess::setStandardOutputFile file locked after QProcess finished
-
Hello,
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.
-
@MNorvalls said in QProcess::setStandardOutputFile file locked after QProcess finished:
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? -
@MNorvalls
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? -
@JonB
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
-
@MNorvalls
I think this is right. If the other process has that file open, and if even you goingQProcess::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!