Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess::setStandardOutputFile file locked after QProcess finished
Forum Updated to NodeBB v4.3 + New Features

QProcess::setStandardOutputFile file locked after QProcess finished

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 737 Views 1 Watching
  • 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 Offline
    M Offline
    MNorvalls
    wrote on last edited by
    #1

    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!

    jsulmJ JonBJ 2 Replies Last reply
    0
    • M MNorvalls

      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!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      2
      • M MNorvalls

        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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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
        0
        • M Offline
          M Offline
          MNorvalls
          wrote on last edited by
          #4

          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

          JonBJ 1 Reply Last reply
          0
          • M MNorvalls

            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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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
            1
            • M Offline
              M Offline
              MNorvalls
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • JonBJ JonB

                @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 Offline
                M Offline
                MNorvalls
                wrote on last edited by
                #7

                @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

                JonBJ 1 Reply Last reply
                0
                • M MNorvalls

                  @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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @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
                  1
                  • JonBJ JonB

                    @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 Offline
                    M Offline
                    MNorvalls
                    wrote on last edited by
                    #9

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

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved