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. Delete QProcess started in MainWindow::closeEvent
Forum Updated to NodeBB v4.3 + New Features

Delete QProcess started in MainWindow::closeEvent

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 910 Views 2 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I have a process that I need to start when I am closing the app and I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

    void MainWindow::closeEvent(QCloseEvent *event)
    {
        QProcess * proc = new QProcess(this);
        proc ->start("program");
    }
    

    I my code the process has MainWindow as it is parent and so I also want to know if, since I am closing the app the MainWindow object will be destroyed along with all its children, there is any chance that the process will be deleted before it's finished.
    In my tests the process always finished successfully but the process depends on the speed of the internet and also of the computer, so if my app is running on a slow computer I am worried if the process might be deleted before it is finished.

    My other option is to use QProcess::startDetached.

    JonBJ Pl45m4P J.HilkJ 3 Replies Last reply
    0
    • H hbatalha

      I have a process that I need to start when I am closing the app and I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

      void MainWindow::closeEvent(QCloseEvent *event)
      {
          QProcess * proc = new QProcess(this);
          proc ->start("program");
      }
      

      I my code the process has MainWindow as it is parent and so I also want to know if, since I am closing the app the MainWindow object will be destroyed along with all its children, there is any chance that the process will be deleted before it's finished.
      In my tests the process always finished successfully but the process depends on the speed of the internet and also of the computer, so if my app is running on a slow computer I am worried if the process might be deleted before it is finished.

      My other option is to use QProcess::startDetached.

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

      @hbatalha said in Delete QProcess started in MainWindow::closeEvent:

      new QProcess(this)

      The parent of this does mean it will get deleted when this (MainWindow) is deleted.

      In any case, if you want a process to survive after your application exits you must use QProcess::startDetached(). Or, you would have to wait till QProcess::finished signal before allowing your main window to be destroyed or your program be exited.

      1 Reply Last reply
      2
      • H hbatalha

        I have a process that I need to start when I am closing the app and I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

        void MainWindow::closeEvent(QCloseEvent *event)
        {
            QProcess * proc = new QProcess(this);
            proc ->start("program");
        }
        

        I my code the process has MainWindow as it is parent and so I also want to know if, since I am closing the app the MainWindow object will be destroyed along with all its children, there is any chance that the process will be deleted before it's finished.
        In my tests the process always finished successfully but the process depends on the speed of the internet and also of the computer, so if my app is running on a slow computer I am worried if the process might be deleted before it is finished.

        My other option is to use QProcess::startDetached.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @hbatalha said in Delete QProcess started in MainWindow::closeEvent:

        I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

        No memory leak, since proc is a child of this

        -> QObject-Tree deletion

        But are you aware that you dont have access to the proc anymore after this is destroyed?! So you cant use QProcess' signals


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        H 1 Reply Last reply
        1
        • H hbatalha

          I have a process that I need to start when I am closing the app and I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

          void MainWindow::closeEvent(QCloseEvent *event)
          {
              QProcess * proc = new QProcess(this);
              proc ->start("program");
          }
          

          I my code the process has MainWindow as it is parent and so I also want to know if, since I am closing the app the MainWindow object will be destroyed along with all its children, there is any chance that the process will be deleted before it's finished.
          In my tests the process always finished successfully but the process depends on the speed of the internet and also of the computer, so if my app is running on a slow computer I am worried if the process might be deleted before it is finished.

          My other option is to use QProcess::startDetached.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @hbatalha said in Delete QProcess started in MainWindow::closeEvent:

          I have a process that I need to start when I am closing the app and I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

          thankfully, the operating system will clean up all your allocations that are associated with your program. That happens regardless of your program exiting normally or if it is ungracefully terminated.

          so your QProcess may be deleted before its finished -> QProcess::startDetached is your only option


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @hbatalha said in Delete QProcess started in MainWindow::closeEvent:

            I want to know if the new object created will be deleted when it is finished or if there will be a memory leak.

            No memory leak, since proc is a child of this

            -> QObject-Tree deletion

            But are you aware that you dont have access to the proc anymore after this is destroyed?! So you cant use QProcess' signals

            H Offline
            H Offline
            hbatalha
            wrote on last edited by hbatalha
            #5

            @Pl45m4 said in Delete QProcess started in MainWindow::closeEvent:

            But are you aware that you dont have access to the proc anymore after this is destroyed?! So you cant use QProcess' signals

            yes I am.

            @JonB @J-Hilk QProcess::startDetached, that is what I thought and following up with another question, can there be case when the process started with QProcess::startDetached will be blocked by an antivirus software since it will not be associated with my app?

            Edit: a question I forgot in the OP: if I don't provide the QProcess with a parent will there be memory leak if the app is closed? This goes to any object newly created with Qt, not just this situation here.

            JonBJ J.HilkJ 2 Replies Last reply
            0
            • H hbatalha

              @Pl45m4 said in Delete QProcess started in MainWindow::closeEvent:

              But are you aware that you dont have access to the proc anymore after this is destroyed?! So you cant use QProcess' signals

              yes I am.

              @JonB @J-Hilk QProcess::startDetached, that is what I thought and following up with another question, can there be case when the process started with QProcess::startDetached will be blocked by an antivirus software since it will not be associated with my app?

              Edit: a question I forgot in the OP: if I don't provide the QProcess with a parent will there be memory leak if the app is closed? This goes to any object newly created with Qt, not just this situation here.

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

              @hbatalha
              In principle, yes, that could of course happen any time. I do not know what your/your AV means by " antivirus software since it will not be associated with my app?", and it would be down to whatever that might or might not implement.

              It would be unlikely to "hang", though. Either the original QProcess would produce errorOccurred on attempting to run, or the process would exit normally immediately. So you can detect these, without hanging exit from your program.

              There can never be a "memory leak if the app is closed" because all memory will be deallocated. However, it is nicer to free things properly (explicitly if not parented) before exit.

              H 1 Reply Last reply
              1
              • H hbatalha

                @Pl45m4 said in Delete QProcess started in MainWindow::closeEvent:

                But are you aware that you dont have access to the proc anymore after this is destroyed?! So you cant use QProcess' signals

                yes I am.

                @JonB @J-Hilk QProcess::startDetached, that is what I thought and following up with another question, can there be case when the process started with QProcess::startDetached will be blocked by an antivirus software since it will not be associated with my app?

                Edit: a question I forgot in the OP: if I don't provide the QProcess with a parent will there be memory leak if the app is closed? This goes to any object newly created with Qt, not just this situation here.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @hbatalha said in Delete QProcess started in MainWindow::closeEvent:

                Edit: a question I forgot in the OP: if I don't provide the QProcess with a parent will there be memory leak if the app is closed? This goes to any object newly created with Qt, not just this situation here.

                @J.Hilk said

                thankfully, the operating system will clean up all your allocations that are associated with your program. That happens regardless of your program exiting normally or if it is ungracefully terminated.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • JonBJ JonB

                  @hbatalha
                  In principle, yes, that could of course happen any time. I do not know what your/your AV means by " antivirus software since it will not be associated with my app?", and it would be down to whatever that might or might not implement.

                  It would be unlikely to "hang", though. Either the original QProcess would produce errorOccurred on attempting to run, or the process would exit normally immediately. So you can detect these, without hanging exit from your program.

                  There can never be a "memory leak if the app is closed" because all memory will be deallocated. However, it is nicer to free things properly (explicitly if not parented) before exit.

                  H Offline
                  H Offline
                  hbatalha
                  wrote on last edited by
                  #8

                  @JonB by " antivirus software since it will not be associated with my app?" I mean for example when the user starts my app the AV might ask the user if it should block my app and the user will probably let my run since he/she knows it and that will tell(I guess) the AV that all processes started under my app are safe. But if I started the process detached and the user gets a warning he/she will not recognize the program and so block it. I hope I made myself clear.

                  There can never be a "memory leak if the app is closed" because all memory will be deallocated. However, it is nicer to free things properly (explicitly if not parented) before exit.

                  This just raised another question for me: see my app might create a number limited by the user of objects during the program life, and those objects will stop being used at any time. So since deleting objects are costly I am letting the OS clean everything up after the app is closed in fear that if I do all those deletions while the program is open it might affect its performance, is this any good?

                  @J-Hilk

                  JonBJ 1 Reply Last reply
                  0
                  • H hbatalha

                    @JonB by " antivirus software since it will not be associated with my app?" I mean for example when the user starts my app the AV might ask the user if it should block my app and the user will probably let my run since he/she knows it and that will tell(I guess) the AV that all processes started under my app are safe. But if I started the process detached and the user gets a warning he/she will not recognize the program and so block it. I hope I made myself clear.

                    There can never be a "memory leak if the app is closed" because all memory will be deallocated. However, it is nicer to free things properly (explicitly if not parented) before exit.

                    This just raised another question for me: see my app might create a number limited by the user of objects during the program life, and those objects will stop being used at any time. So since deleting objects are costly I am letting the OS clean everything up after the app is closed in fear that if I do all those deletions while the program is open it might affect its performance, is this any good?

                    @J-Hilk

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

                    @hbatalha

                    But if I started the process detached and the user gets a warning he/she will not recognize the program and so block it. I hope I made myself clear.

                    No, you don't make it clear. I have no idea what this means, and sounds like AV-specific behaviour anyway.

                    I am letting the OS clean everything up after the app is closed in fear that if I do all those deletions while the program is open it might affect its performance, is this any good?

                    No, do your deletions in program. Unless you have a very good reason not to do so, which will not be the case. They don't take that long, and using up more & more memory is worse.

                    H 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @hbatalha

                      But if I started the process detached and the user gets a warning he/she will not recognize the program and so block it. I hope I made myself clear.

                      No, you don't make it clear. I have no idea what this means, and sounds like AV-specific behaviour anyway.

                      I am letting the OS clean everything up after the app is closed in fear that if I do all those deletions while the program is open it might affect its performance, is this any good?

                      No, do your deletions in program. Unless you have a very good reason not to do so, which will not be the case. They don't take that long, and using up more & more memory is worse.

                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #10

                      @JonB said in Delete QProcess started in MainWindow::closeEvent:

                      sounds like AV-specific behaviour anyway.

                      Actually this is just a scenario I am making up in my head. I am not sure this actually happen. So just forget it.

                      No, do your deletions in program. Unless you have a very good reason not to do so, which will not be the case. They don't take that long, and using up more & more memory is worse.

                      Thanks, I will do that.

                      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