wx.MessageDialog.Center causes segmentation fault on 4.1.1

I’ve run into a segmentation fault with wxPython 4.1.1 when I call Center or Centre on wx.MessageDialog. No segmentation fault with wxPython 4.1.0 (but look like this isn’t centering the window either?).

Maybe I’m not supposed to call Center before showing, but I’m not sure how else to center a modal?

import wx

DO_CENTER = True


class MainFrame(wx.Frame):
    def __init__(self, *args):
        wx.Frame.__init__(self, *args)

        msg = "wxPython %s\nCenter Called: %s" % (wx.version(), DO_CENTER)
        self.dlg = wx.MessageDialog(self, msg, "Diaglog Center Test")
        if DO_CENTER:
            self.dlg.Center()
        self.dlg.ShowModal()
        self.dlg.Destroy()


class MainApp(wx.App):
    def OnInit(self):
        self.frame = MainFrame(None, wx.ID_ANY, "")
        self.frame.Show()
        return True


def start():
    app = MainApp()
    app.MainLoop()


if __name__ == "__main__":
    start()

I guess I’ve already answered my own question, I should pass wx.CENTER into the style flag for wx.MessageDialog.

Any insight into why there’s a segmentation fault as of 4.1.1 though?

Besides the somewhat special composition there is no problem on a Windows (always newest) :ghost:

Yes, confirmed no issues on MSW7 either.

wx.MessgeDialog is a native dialog, which means it is not a wx.Dialog despite the fact that the class derives from wx.Dialog. In a nutshell, it’s really just a shim that adapts the typical dialog methods to platform APIs that create and show the native dialog.

What this really means is that, at best, many of the wx.Dialog or wx.Window methods, events, etc. are simply ignored. In this case it results in a crash because of GetPeer returning NULL here:

double wxWindowMac::GetContentScaleFactor() const 
{
    return GetPeer()->GetContentScaleFactor();
}

(Please create a bug report for this at trac.wxwidgets.org. It should at least not crash.)

But the key takeaway here is that native dialogs are not actually wx.Windows, so all you can be sure of working fully is creating it and calling ShowModal. There will be more coverage in some cases, but if you need full functionality then you’ll need to use a generic version or self-implemented dialog instead.

1 Like

It’s my first ticket, hopefully I didn’t screw it up :slight_smile:
https://trac.wxwidgets.org/ticket/19038#ticket