SEGV_MAPERR for PyQt5 object

Viewed 88

Why does the following demo code produce a SEGV_MAPERR? How would one fix it?

Hint: Once one deletes the line annotated with "# Ref1", no error is produced. This is not easily done in the production code the problem is abstracted from.

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebChannel

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
        self.webView.setHtml("")
        self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        page = self.webView.page() # REF1
        for i in range(2):
            self.init_page()

    def init_page(self):
        class EditObject(QtCore.QObject):
            @QtCore.pyqtSlot(str)
            def edit(self, s):
                print("test")
        editObject = EditObject(self.webView.page())
        poChannel = self.webView.page().webChannel()
        print(1)
        if poChannel is None:
            poChannel = QtWebChannel.QWebChannel()
            self.webView.page().setWebChannel(poChannel)
        print(2)
        objects = poChannel.registeredObjects()
        print(objects)
        poChannel.registerObject("editObject", editObject)
        self.webView.setHtml("")

from PyQt5 import QtWebEngineWidgets

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

This is similar to Issues with PyQt5==5.10 on Ubuntu 18, but with example code. For the example code, https://gist.github.com/gioditalia/03c9fd5d793aeccbe065fea45d842431 is adapted.

1 Answers

The problem is that poChannel is a local variable that will be deleted after executing init_page, so in the second iteration the poChannel reference will point to an incorrect memory address. So the solution is to extend its cycle to that of the view, so we take advantage of the Qt property and pass it as a parent to self.webView.

poChannel = QtWebChannel.QWebChannel(self.webView)

Although as PyQt points out in the docs and the generated file: # WARNING! All changes made in this file will be lost!, it is not convenient to modify the class generated by the .ui, instead you must create another class that inherits from the appropriate widget and use the interface provided by Qt Designer.

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets, QtWebChannel

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
        self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

class EditObject(QtCore.QObject):
    @QtCore.pyqtSlot(str)
    def edit(self, s):
        print("test")

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        for i in range(2):
            self.init_page()

    def init_page(self):
        editObject = EditObject(self.webView.page())
        poChannel = self.webView.page().webChannel()
        if poChannel is None:
            poChannel = QtWebChannel.QWebChannel(self)
            self.webView.page().setWebChannel(poChannel)
        objects = poChannel.registeredObjects()
        poChannel.registerObject("editObject", editObject)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
Related