Closed sander76 closed 5 years ago
Hi sander76, I took a close look. Pypubsub holds only weak reference to the listeners it registers, so that when a listener is no longer used by your app, it can be released from memory (if pubsub stored strong ref, the listener would remain alive and registered even when nothing else in the app referenced it, which can cause mem leak in some cases).
Looking at your code, every thing seems to be holding strong references in the hierarchy leading to pressed
listener EXCEPT that controller variable in the Mainframe.__init__
is not being saved as instance variable. This means that at the end of init, controller will be garbaged, and pubsub will automatically release it. The fact that your debugger indicates that it is in fact not dead may be false-positive, as the debugger can hold strong reference in some situations that interfere with normal pubsub lifecycle management.
If changing to self.controller = ...
does not help, try clicking on the button multiple times. If you never get the event, or get it only once, then weak referencing is at play. If called every time, then the listener is in fact alive and well and there must be a bug somewhere.
@schollii Thanks! Your suggestion self.controller =
did the trick.
Thanks again !
I am trying to implement the MVC pattern using Python3.7, wxpython (4.0.4) and Pypubsub 4.0.3.
See below a code example. When I run this the following gets printed to the console:
Last line shows Listener of Topic "pressed" has died. I don't understand why. Also when I run this through the debugger (pycharm) and put a breakpoint where the listener is created, wait a bit and continue the Listener doesn't die and all works as it should. Any suggestion whether this might be a bug, or am I doing something wrong ?