[PyQt] deleteLater vs setParent(None)
Elvis Stansvik
elvstone at gmail.com
Wed May 11 08:24:00 BST 2016
2016-05-10 18:42 GMT+02:00 Dave Gradwell <davegradwell at yahoo.co.uk>:
> Hi Elvis,
>
> I’m doing something similar which works for me in production if that helps at all.
> I preferred the explicitness of deleteLater, although I think either technique would work?
>
> Best, Dave.
Thanks a lot for sharing your code Dave, looks very similar indeed. I
also like the explicitness of deleteLater better.
The reason I asked is precisely this wording by Phil:
"Changing the ownership also removes that extra reference so that the
object is available to be garbage collected when it goes out of
scope."
I'm not sure if that means deleteLater will not be effective because
of this extra reference that PyQt keeps or not? But thinking about it
a bit more, it probably will be effective, since I guess if the object
is deleted on the C++ side (deleteLater), and PyQt discovers that the
only reference is its own, then it will let go of that reference to
let Python object be garbage collected?
Phil, would you care to comment on this? I realize I don't have a full
understanding of how this all works yet.
My goal is to not accumulate memory in this slot, so I'd like both the
underlying QProcess object to be cleaned up, as well as any Python
instances of my subclass Process.
Elvis
>
>
> “”””””””””””””””””"
> class Process(QtCore.QProcess):
> def __init__(self, singleshot=True, *args, **kwargs):
> super().__init__(*args, **kwargs)
> self.finished.connect(self.on_finished) # Important this is the first Slot for the Finished Signal (so that our outputText (etc) are readied).
> if singleshot:
> self.finished.connect(lambda: self.deleteLater())
>
> self.__outputText = ""
> self.__errorText = ""
> self.__succeeded = False
>
> def on_finished(self, exitCode, exitStatus):
> self.__outputText = str(self.readAllStandardOutput(), encoding='utf-8')
> self.__errorText = str(self.readAllStandardError(), encoding='utf-8')
> self.__succeeded = (exitCode == 0) & (exitStatus == self.NormalExit)
>
> def outputText(self):
> return self.__outputText
>
> def errorText(self):
> return self.__errorText
>
> def succeeded(self):
> return self.__succeeded
>
> def failed(self):
> # Slightly wary about this wording: for instance, returns true before even starting...
> return not self.__succeeded
>
>
> class selfTerminatingProcess(Process):
> def __init__(self, timeToLive=1000, *args, **kwargs):
> super().__init__(*args, **kwargs)
> self.startTimer(timeToLive)
>
> def timerEvent(self, QTimerEvent):
> self.killTimer(QTimerEvent.timerId())
> self.close()
> “”””””””””””””””””"
>
>
>
>
>> On 10 May 2016, at 09:21, Elvis Stansvik <elvstone at gmail.com> wrote:
>>
>> My question is about:
>>
>> process.finished.connect(process.deleteLater)
>>
>> Can I count on that the Process object being destroyed eventually
>> here, or should I rather use
>>
>> def cleanup():
>> process.setParent(None)
>>
>> process.finished.connect(cleanup)
>>
>> That is, setting its parent to None.
>
> _______________________________________________
> PyQt mailing list PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
More information about the PyQt
mailing list