[PySide] Is del in python completely equivalent to delete in c for Qt objects?

Israel Brewster ijbrewster at alaska.edu
Thu Feb 6 21:41:33 CET 2020


I must apologize. Apparently I am horrible at asking questions.

On Feb 6, 2020, at 11:28 AM, John Ehresman <jpe at wingware.com> wrote:
> 
> In general, del in Python is very different than delete in C++.  I'll leave it to others to explain.  

In general, sure. In general, python memory management is very different than C/C++ memory management, so it stands to reason that del/delete would, in general, be different as well. I’m not asking about the general case, I’m asking about the Qt case, and the things that Qt does when you delete a Qt object. I’m not asking for a low-level view of memory management. I’m asking if the *behavior*, as far as *Qt* objects is concerned, is the same.

> For the Qt case, you may want to look at QObject.deleteLater()

Unfortunately, that only works if you are running an event loop. As my program has no event loop of any sort, the deleteLater() would never be executed.

> or PySide may expose a function to do a C++ delete -- it's probably shiboken.delete().

That sounds like what I’m looking for, assuming that a python del doesn’t do the same thing.

> 
> You are correct that deleting a QObject will also delete all of it's child objects.

In C/C++, yes. But that’s the question: does calling del on a QObject in python code do the same thing?
---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell:  907-328-9145

> 
> John
> 
> On 2/7/20 7:57 AM, Israel Brewster wrote:
>>> On Feb 6, 2020, at 7:55 AM, Jason H <jhihn at gmx.com <mailto:jhihn at gmx.com> <mailto:jhihn at gmx.com <mailto:jhihn at gmx.com>>> wrote:
>>> 
>>> It is subject to reference counting semantics in python.
>>> #declare and and b as object with properties b and a, respectively, then do:
>>> a.b = b
>>> b.a = a
>>> a and b are now impossible to delete. Next:
>>> a.b = None
>>> del(a) #nope
>>> del(b) #ok
>> Ok, yeah, that’s a good point - there are cases where you can run into problems in python that perhaps wouldn’t be an issue in C. However my question is about the “normal” case, where you are just deleting a basic Qt object that has children objects. In C, this results in the deletion of the child objects as well, due to some Qt “magic”. So I can just call delete on the top-level object, and know that all children will be deleted as well. Is the same true in Python with PySide2, such that calling del in python on a Qt object is exactly equivalent to calling delete on an identical object in C, assuming no corner cases?
>> For a little background, I have an application that makes heavy use of multiprocessing to be able to generate images (using QWidgets to “build” the image) and save them to disk in parallel. The creation of the widgets HAS to be in parallel, thus multiprocessing. However, it seems that if I do any work with QWidgets on the main process, that somehow pollutes the main process such that the child processes now crash at some point (and it’s REALLY difficult to track down where). On the other hand, if I *don’t* do anything with QApplication/QWidgets in the main process, the child processes work fine. So I’m trying to make sure I’m “cleaning up” properly to avoid crashing after doing stuff in the main process, but to do that I need to be sure everything I created is properly deleted - including the main QApplication.
>> And yes, I know you can only create widgets on the main thread. Please note that I am NOT using threads, as threads do not allow true multitasking. I am using processes, and only working with widgets on the main thread of any given process. So that much works, and is fine.
>> ---
>> Israel Brewster
>> Software Engineer
>> Alaska Volcano Observatory
>> Geophysical Institute - UAF
>> 2156 Koyukuk Drive
>> Fairbanks AK 99775-7320
>> Work: 907-474-5172
>> cell:  907-328-9145
>>> *Sent:* Wednesday, February 05, 2020 at 7:23 PM
>>> *From:* "Israel Brewster" <ijbrewster at alaska.edu <mailto:ijbrewster at alaska.edu> <mailto:ijbrewster at alaska.edu <mailto:ijbrewster at alaska.edu>>>
>>> *To:* pyside at qt-project.org <mailto:pyside at qt-project.org> <mailto:pyside at qt-project.org <mailto:pyside at qt-project.org>>
>>> *Subject:* [PySide] Is del in python completely equivalent to delete in c for Qt objects?
>>> In C++ Qt code, if I do something like the following:
>>> QMainWindow *win=new QMainWindow()
>>> <add other objects to win/do stuff with it>
>>> delete win
>>> The QMainWindow, as well as all of its children will be deleted, with the latter half of that statement being the important part. Of course, this child deletion is a Qt thing and not a C thing, as typically calling delete only deletes the thing it was called on. As such, I was wanting to confirm that, when using python/PySide2, calling del on a Qt object would trigger the same child deletion algorithm that delete does in C.
>>> ---
>>> Israel Brewster
>>> Software Engineer
>>> Alaska Volcano Observatory
>>> Geophysical Institute - UAF
>>> 2156 Koyukuk Drive
>>> Fairbanks AK 99775-7320
>>> Work: 907-474-5172
>>> cell:  907-328-9145
>>> _______________________________________________ PySide mailing list PySide at qt-project.org <mailto:PySide at qt-project.org> <mailto:PySide at qt-project.org <mailto:PySide at qt-project.org>> https://lists.qt-project.org/listinfo/pyside <https://lists.qt-project.org/listinfo/pyside>
>> _______________________________________________
>> PySide mailing list
>> PySide at qt-project.org <mailto:PySide at qt-project.org>
>> https://lists.qt-project.org/listinfo/pyside <https://lists.qt-project.org/listinfo/pyside>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20200206/bd84b50c/attachment-0001.html>


More information about the PySide mailing list