wxFormBuilder icon indicating copy to clipboard operation
wxFormBuilder copied to clipboard

UnUnit() of AUI should not be in __del__ of the MainFrame (Python code generation)

Open dhyams opened this issue 5 years ago • 5 comments

self.m_mgr.UnInit() should be moved to the OnClose event handler, rather than being in the del magic method for the frame. An app using AUI will get this message:

Traceback (most recent call last): File "mygui.py", line 175, in app.MainLoop() File "C:\venvs\mygui\lib\site-packages\wx\core.py", line 2224, in MainLoop rv = wx.PyApp.MainLoop(self) wx._core.wxAssertionError: C++ assertion "GetEventHandler() == this" failed at ....\src\common\wincmn.cpp(475) in wxWindowBase::~wxWindowBase(): any pushed event handlers must have been removed

This is further discussed here; I ran into exactly the same problem: https://discuss.wxpython.org/t/aui-error-any-pushed-event-handlers-must-have-been-removed/34555

The discussion linked above solves the problem, so the wxFormBuilder generated just needs to match the recommendation made there by Robin Dunn.

dhyams avatar Jul 15 '20 21:07 dhyams

I don't know any Python, in C++ it is sufficient to uninitialize in the destructor because of destructor chaining this call will be executed before the base class destructor that performs this check. Are you sure the problem is that this call doesn't get executed? Or did your user code push an own handler and you forgot to remove it so that the automatic removal sees the wrong event handler?

Putting that cleanup code into the close event handler does not work because it interferes with user code. If the user doesn't define that handler it doesn't exist. If the generator defines one and the user code as well, how can you ensure the generator one does get executed in the correct order (after the user code one)?

I somehow doubt that in Python no chaining of destructors is performed, how does proper cleanup of objects with inheritance work there? Or is it a wxPython problem that works differently than in C++ and you have to cleanup AUI in another way?

sodevel avatar Jul 16 '20 16:07 sodevel

The issue (as I grok it from the wxpython discussion thread linked in the OP) is that the UnUnit comes too late; that by the time del is called, the GUI has been destroyed. I defer to Robin Dunn on this one (author of wxPython), but this is basically what he says in the discussion thread. And it does work to move the UnInit() to Close().

I fully sympathize with your second paragraph. Bottom line is that yes, you can define work to do in OnClose(), but it will still be up to the user's override of OnClose (if they have one) to call it.

I think, but I'm not sure, that yes, cleaning up AUI has to work differently in Python than in C++.

dhyams avatar Jul 16 '20 16:07 dhyams

I hope that PR Call wxAuiManager::UnInit() automatically will be merged for new release so in new code one won't need call UnInit every time at all. As far I understand wxPython is very active binding so those change will be apply soon if they appear in wxWidgets repo.

Kvaz1r avatar Jul 18 '20 18:07 Kvaz1r

@Kvaz1r I see that 12 days ago, the PR you reference was merged.

dhyams avatar Aug 01 '20 13:08 dhyams

In that case - just wait while 3.1.4 will be ported to wxPython.

Kvaz1r avatar Aug 01 '20 15:08 Kvaz1r