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.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 theMainWindow
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
. -
@hbatalha said in Delete QProcess started in MainWindow::closeEvent:
new QProcess(this)
The parent of
this
does mean it will get deleted whenthis
(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 tillQProcess::finished
signal before allowing your main window to be destroyed or your program be exited. -
@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 ofthis
-> QObject-Tree deletion
But are you aware that you dont have access to the
proc
anymore afterthis
is destroyed?! So you cant useQProcess
' signals -
@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
-
@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 withQProcess::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
new
ly created with Qt, not just this situation here. -
@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 produceerrorOccurred
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.
-
@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.
-
@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?
-
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.
-
@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.