Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QProcess::setStandardOutputFile file locked after QProcess finished

    General and Desktop
    3
    9
    237
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      MNorvalls last edited by

      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!

      jsulm JonB 2 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @MNorvalls last edited by

        @MNorvalls Did you make sure the process really terminated? It sounds like the process is still running and thus keeping the file open.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 2
        • JonB
          JonB @MNorvalls last edited by

          @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?

          1 Reply Last reply Reply Quote 0
          • M
            MNorvalls last edited by

            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.

            bb7f06ec-9df0-4cca-9076-52b8acf17cc8-image.png

            JonB 1 Reply Last reply Reply Quote 0
            • JonB
              JonB @MNorvalls last edited by

              @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?

              M 1 Reply Last reply Reply Quote 1
              • M
                MNorvalls last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • M
                  MNorvalls @JonB last edited by

                  @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

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @MNorvalls last edited by

                    @MNorvalls
                    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.

                    M 1 Reply Last reply Reply Quote 1
                    • M
                      MNorvalls @JonB last edited by

                      @JonB

                      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!

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post