[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] memory management
From:       Maurizio Berti <maurizio.berti () gmail ! com>
Date:       2018-08-27 22:54:19
Message-ID: CAPn+-XSQkh+etMvEAcm2fRuVVKRAxxoj=zWNYKgCR1oaTSQiRw () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Python automatically takes care of memory using its garbage collector,
meaning that it (should) delete an object when it thinks that that object
is not needed anymore. This is really convenient, as you don't usually need
to care about deleting [Q]Objects unless you really have a good reason to
do that.

But.
PyQt is a wrapper, and things can get messy sometimes.
This means that there are situations in which an object might still exist
as a Python wrapped object while its C++ counterparts don't. Also, Python
"needs" a QObject [sub]class to be properly initialized only to use
QObject's Qt C++ wrapped methods, so a QObject Python class might exist
even if its C++ object doesn't.

Anyways, usually you give a parent to an object when you really need it to
have a parent (for example, because you need to easily access it using
parent()), unless Qt takes care of it, like when adding widgets to layouts;
otherwise, it's enough to set it as another object's attribute or property,
including basic objects like lists or dictionaries: if you delete that
"main" object and it's QObjects attributes are not parented with or used by
another object, they will be deleted along with it.
This obviously means that a QObject deleted using deleteLater() might still
exist as a Python object, for example, because it's stored in a list
somewhere else.
Theoretically this also means that the memory used by the wrapped QObject
should be freed after deleteLater(), leaving only the Python object in
memory (until the object is actually deleted with del).

An interesting article about this:
http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html

Maurizio
PS: I'm not an expert in low-level programming and memory management,
please correct me if I wrote anything wrong.


2018-08-27 21:27 GMT+02:00 Christopher Probst <christop.probst@gmail.com>:

> Hello,
>
> I am from the C++ world. Is there still a reason to provide a parent to a
> QObject in pyQt to ensure that children are deleted when a QObject is
> deleted?
>
> Thanks,
> Christopher
>
> _______________________________________________
> PyQt mailing list    PyQt@riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net

[Attachment #5 (text/html)]

<div dir="ltr">Python automatically takes care of memory using its garbage collector, \
meaning that it (should) delete an object when it thinks that that object is not \
needed anymore. This is really convenient, as you don&#39;t usually need to care \
about deleting [Q]Objects unless you really have a good reason to do \
that.<div><br></div><div>But.</div><div>PyQt is a wrapper, and things can get messy \
sometimes.<br>This means that there are situations in which an object might still \
exist as a Python wrapped object while its C++ counterparts don&#39;t. Also, Python \
&quot;needs&quot; a QObject [sub]class to be properly initialized only to use \
QObject&#39;s Qt C++ wrapped methods, so a QObject Python class might exist even if \
its C++ object doesn&#39;t.</div><div><br></div><div>Anyways, usually you give a \
parent to an object when you really need it to have a parent (for example, because \
you need to easily access it using parent()), unless Qt takes care of it, like when \
adding widgets to layouts; otherwise, it&#39;s enough to set it as another \
object&#39;s attribute or property, including basic objects like lists or \
dictionaries: if you delete that &quot;main&quot; object and it&#39;s QObjects \
attributes are not parented with or used by another object, they will be deleted \
along with it.</div><div>This obviously means that a QObject deleted using \
deleteLater() might still exist as a Python object, for example, because it&#39;s \
stored in a list somewhere else.<br>Theoretically this also means that the memory \
used by the wrapped QObject should be freed after deleteLater(), leaving only the \
Python object in memory (until the object is actually deleted with \
del).</div><div><br></div><div>An interesting article about this:<br><a \
href="http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html">http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html</a><br></div><div><br></div><div>Maurizio<br>PS: \
I&#39;m not an expert in low-level programming and memory management, please correct \
me if I wrote anything wrong.</div><div><br></div></div><div \
class="gmail_extra"><br><div class="gmail_quote">2018-08-27 21:27 GMT+02:00 \
Christopher Probst <span dir="ltr">&lt;<a href="mailto:christop.probst@gmail.com" \
target="_blank">christop.probst@gmail.com</a>&gt;</span>:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I am from the C++ \
world. Is there still a reason to provide a parent to a QObject in pyQt to ensure \
that children are deleted when a QObject is \
deleted?</div><div><br></div><div>Thanks,</div><div>Christopher</div></div> \
<br>______________________________<wbr>_________________<br> PyQt mailing list      \
<a href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.<wbr>riverbankcomputing.com/<wbr>mailman/listinfo/pyqt</a><br></blockquote></div><br><br \
clear="all"><div><br></div>-- <br><div class="gmail_signature" \
data-smartmail="gmail_signature">È difficile avere una convinzione precisa quando si \
parla delle ragioni del cuore. - &quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a \
href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div> </div>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic