Reputation: 4967
I get a strange behavior that's happening on OSX but not on Windows.
I made a software with pyqt5 and when I close a child window I have a crash with "Segmentation fault: 11"
To explain better, I have a main window where I may open other windows to ask to the user some info. the error is happening when I close that second window.
I have a closeEvent
function :
def closeEvent(self, event):
self.Close_OBJ.emit()
self.close()
which send a pyqtSignal
to the main window.
If I comment the self.Close_OBJ.emit()
I don't have the Segmentation fault anymore, but I need this so my main window can react to the closing of that child window.
What is also confusing is that I have other child windows where I have the same closeEvent
function and it's work well.
I don't understand why I don't have this issue on Windows OS.
Here's a minimal example of the issue:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class Modify_1_NMM(QMainWindow):
Close_OBJ = pyqtSignal()
def __init__(self,):
super(Modify_1_NMM, self).__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QHBoxLayout()
self.label = QLabel('Please, close this window')
self.mainHBOX_param_scene.addWidget(self.label)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
def closeEvent(self, event):
self.Close_OBJ.emit()
self.close()
class StimEdit(QMainWindow):
def __init__(self, parent=None):
super(StimEdit, self).__init__()
self.NewModifyXNMM = None
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QHBoxLayout()
self.B = QPushButton('clik here to open o window')
self.B.setFixedSize(400,200)
self.B.clicked.connect(self.ModXNMMclicked)
self.mainHBOX_param_scene.addWidget(self.B)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
def ModXNMMclicked(self,):
if self.NewModifyXNMM == None:
self.NewModifyXNMM = Modify_1_NMM()
self.NewModifyXNMM.Close_OBJ.connect(self.close_ModXNMM)
self.NewModifyXNMM.show()
@pyqtSlot()
def close_ModXNMM(self):
print('about to close',self)
self.NewModifyXNMM = None
print('did closed')
def main():
app = QApplication(sys.argv)
ex = StimEdit(app)
ex.show()
sys.exit(app.exec_( ))
if __name__ == '__main__':
main()
when I click on the button on the main window, a second window opens. When I then close the second window, it crashes (not even all the time... but most of the time).
On pycharm I get this error message :
about to close <__main__.StimEdit object at 0x103f0af78>
did closed
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
and on the terminal I get:
about to close <__main__.StimEdit object at 0x10c706288>
did closed
Segmentation fault: 11
if I comment out the line self.NewModifyXNMM = None
I don't have the segmentation fault anymore. Is it possible this line is in conflict with the window closing?
Upvotes: 1
Views: 1341
Reputation: 243983
It seems that the object reference is being lost when you set a None to the variable, a possible workaround is to use deleteLater and the destroyed signal:
@pyqtSlot()
def close_ModXNMM(self):
print('about to close',self)
self.NewModifyXNMM.deleteLater()
self.NewModifyXNMM.destroyed.connect(self.on_destroyed)
print('did closed')
@pyqtSlot('QObject*')
def on_destroyed(self, o):
self.NewModifyXNMM = None
print(self.NewModifyXNMM)
Upvotes: 1