Search code examples
pythonpython-2.7wxpythonwxwidgets

wxPython check if the c++ part is deleted


I am working on a project where a function is throwing a wx.pyDeadObject Error because of a delayed task.

I've read that in wx you can check if the c++ object still exsists by running if self:, however this no longer works in wxPython 3.0.2. using wx 3.

I've modified the function to the following:

def SetData(self, delayedResult):
    if not self or not self.list:
        return
    data = []
    torrents = delayedResult.get()

    for torrent in torrents:
        data.append((torrent.infohash, [torrent.name], torrent, ThumbnailListItemNoTorrent))

    self_exist = True
    list_exists = True

    if not self:
        self_exist = False
    if not self.list:
        list_exists = False

    try:
        self.list.SetData(data)
        self.list.SetupScrolling()
    except wx.PyDeadObjectError:
        print "Self existed: %s and self.list existed: %s" % (self_exist, list_exists)

It already passes the first if statement, but since the delayedResutlt.get() is blocking I added the additional try except (I also tried duplicating that if statement at the start of the function, but that didn't work).

When running one of the unit tests I got Self existed: True and self.list existed: True which confirmed my suspicions. The documentation of 3.0.3 says this should be present.

How can you test if the c++ part of a wx object has been deleted in wxPython?


Solution

  • Turns out that you first have to wx.Yield() after having called Destroy() or something alike, in contrast to wx 2.8. The events then get processed and destroyed.

    In all cases, using bool(object) will evaluate to True or False depending on if the object is still active.