Tutorial: Segmentation fault: 11

I have been going through Jan Bodnar’s wxPython tutorial. The examples have worked fine so far, but with the checkmenu_item.py example on the “Menus and toolbars” sub page I get:

Segmentation fault: 11

I was able to bypass the problem by adding print lines to the code until it started working for some reason. According to Google searches I have a hunch this could be related to Mac compatibility issues with Python, but as a newbie with wxPython it could be an entirely different reason, but anyway, I’m mentioning my versions: OS X El Capitan 10.11.6 and Python 3.6.2 (and Python 2.7.10). I use MacPorts 2.6.2 in case that matters.

Since wxWidgets is a C++ library and wxPython is a fairly thin wrapper around that, we have to be a little more careful and use more “defensive programming” than we normally would with pure Python code.

If you look at the crash report that macOS gives you then you can find the C++ stack trace that led to the crash. In this case it has text that looks like something like this:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x0000000108fcd880 wxBitmapRefData::GetImage() const + 16 (bitmap.cpp:354)
1   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x0000000108fce5fd wxBitmap::GetImage() const + 29 (bitmap.cpp:930)
2   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x00000001090c9745 wxBitmap::GetNSImage() const + 21 (bitmap.h:206)
3   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x0000000109102074 wxToolBarTool::UpdateImages() + 52 (toolbar.mm:600)
4   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x000000010910704b wxToolBar::DoInsertTool(unsigned long, wxToolBarToolBase*) + 2603 (toolbar.mm:1542)
5   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x00000001092b18aa wxToolBarBase::InsertTool(unsigned long, wxToolBarToolBase*) + 202 (tbarbase.cpp:204)
6   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x00000001092b1744 wxToolBarBase::DoInsertNewTool(unsigned long, wxToolBarToolBase*) + 52 (tbarbase.h:671)
7   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x00000001092b16f4 wxToolBarBase::InsertTool(unsigned long, int, wxString const&, wxBitmap const&, wxBitmap const&, wxItemKind, wxString const&, wxString const&, wxObject*) + 324 (tbarbase.cpp:189)
8   libwx_osx_cocoau_core-3.1.4.0.0.dylib	0x00000001092b159d wxToolBarBase::DoAddTool(int, wxString const&, wxBitmap const&, wxBitmap const&, wxItemKind, wxString const&, wxString const&, wxObject*, int, int) + 189 (tbarbase.cpp:172)
9   _core.cpython-37m-darwin.so   	0x0000000108420918 wxToolBarBase::AddTool(int, wxString const&, wxBitmap const&, wxBitmap const&, wxItemKind, wxString const&, wxString const&, wxObject*) + 152 (tbarbase.h:300)
10  _core.cpython-37m-darwin.so   	0x0000000108420a7e wxToolBarBase::AddTool(int, wxString const&, wxBitmap const&, wxString const&, wxItemKind) + 190 (tbarbase.h:311)
11  _core.cpython-37m-darwin.so   	0x0000000108417768 meth_wxToolBar_AddTool(_object*, _object*, _object*) + 792 (sip_corewxToolBar.cpp:1004)

At the bottom we can see that it starts with a call to the toolbar’s AddTool method, and that it continues on to code that is trying to convert the bitmap to an image. If you then look back at the Python code you’ll find this:

        self.toolbar.AddTool(1, '', wx.Bitmap('texit.png'))

But you likely do not have a texit.png file in your current working directory. (You can probably download it from somewhere on ZetCode.) A more defensive version of the code could be implemented like this:

        bmp = wx.Bitmap('texit.png')
        assert bmp.IsOk()
        self.toolbar.AddTool(1, '', bmp)

Should I ask this about in a different thread or is this thread okay: in the context_menu.py example on the same page, if I select “Close” in the context menu I get:

Python(2181,0x10172b000) malloc: *** error for object 0x11171c1d0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Should I use some kind of protective tactic with this problem also?

That appears to be a bug that is already fixed in the 4.1 builds. In the meantime, you can work around it by delaying the call to self.parent.Close with wx.CallAfter, like this:

    def OnClose(self, e):
        wx.CallAfter(self.parent.Close)

In the checkmenu_item.py on the same page, the example application has an “Enter fullscreen” option on Mac, but there is no code I can find in the source code to show it. This seems like something that will be shown as default. Is there a way to hide (or show if needed in another app) the “Enter fullscreen” option from the menu?

I don’t see anything about fullscreen on that page, so maybe we’re not looking at the same thing. But perhaps you are looking for this:
https://docs.wxpython.org/wx.TopLevelWindow.html#wx.TopLevelWindow.Maximize

or this:
https://docs.wxpython.org/wx.TopLevelWindow.html#wx.TopLevelWindow.ShowFullScreen

You already gave the answer, but here is a screenshot where you can see the “Enter Full Screen” option.

That menu item is added by OSX, not the application. There are a few platform-standard things like that on OSX that will be added to appropriate menus, (Edit, Window, Help, etc.) if they are present.