Skip to content

Qt binding: crash due to Python allocation-while-deallocating #1054

@klayoutmatthias

Description

@klayoutmatthias

While executing the testcase from https://www.klayout.de/forum/discussion/2052/qabstracttablemodel-does-not-draw-checkbox-for-checkable-items#latest multiple times, the application crashes.

The reason is: when the window is re-created, Python will destroy the existing one. This happens in Py_Dealloc. However, the destruction will trigger a disconnectNotify while in Py_Dealloc and this tries to create a Python object. Apparently, Python cannot handle Py_Alloc calls during Py_Dealloc properly and the application will crash after a few rounds.

Here is the stack trace:

1  __GI_raise                                                                                                                                                 raise.c                       50   0x7fffe5cde18b 
2  __GI_abort                                                                                                                                                 abort.c                       79   0x7fffe5cbd859 
3  fatal_error                                                                                                                                                pylifecycle.c                 2183 0x7ffff7ba6e94 
4  Py_FatalError                                                                                                                                              pylifecycle.c                 2193 0x7ffff7ba9c09 
5  _PyObject_AssertFailed                                                                                                                                     object.c                      2200 0x7ffff7ab242e 
6  update_refs                                                                                                                                                gcmodule.c                    350  0x7ffff7bd07a9 
7  collect                                                                                                                                                    gcmodule.c                    1053 0x7ffff7bd14ef 
8  collect_with_callback                                                                                                                                      gcmodule.c                    1240 0x7ffff7bd1cf5 
9  collect_generations                                                                                                                                        gcmodule.c                    1262 0x7ffff7bd1f79 
10 _PyObject_GC_Alloc                                                                                                                                         gcmodule.c                    1988 0x7ffff7bd2075 
11 _PyObject_GC_Malloc                                                                                                                                        gcmodule.c                    1998 0x7ffff7bd2c6d 
12 PyType_GenericAlloc                                                                                                                                        typeobject.c                  1015 0x7ffff7ad055a 
13 pya::object_to_python                                                                                                                                      pyaConvert.cc                 483  0x7fffef16510d 
14 pya::object_to_python                                                                                                                                      pyaConvert.cc                 389  0x7fffef164db7 
15 pya::reader<gsi::ObjectType>::operator()                                                                                                                   pyaMarshal.cc                 784  0x7fffef18ee5b 
16 gsi::call_variadic_function<pya::reader<gsi::ObjectType>, gsi::SerialArgs *, pya::PythonRef *, pya::PYAObjectBase *, gsi::ArgType, tl::Heap *>::operator() gsiTypes.h                    2020 0x7fffef186fd9 
17 gsi::do_on_type_impl<pya::reader, gsi::SerialArgs *, pya::PythonRef *, pya::PYAObjectBase *, gsi::ArgType, tl::Heap *>                                     gsiTypes.h                    2168 0x7fffef18696e 
18 gsi::do_on_type<pya::reader>::operator()<gsi::SerialArgs *, pya::PythonRef *, pya::PYAObjectBase *, gsi::ArgType, tl::Heap *>                              gsiTypes.h                    2381 0x7fffef170403 
19 pya::pop_arg                                                                                                                                               pyaMarshal.cc                 805  0x7fffef16f1f2 
20 pya::Callee::call                                                                                                                                          pyaObject.cc                  148  0x7fffef192aa2 
21 gsi::Callback::call_int                                                                                                                                    gsiCallback.h                 70   0x7fffefa16a6c 
22 gsi::Callback::issue<QAbstractTableModel_Adaptor, QMetaMethod const&>                                                                                      gsiCallbackVar.h              38   0x7fffefb3f717 
23 QAbstractTableModel_Adaptor::disconnectNotify                                                                                                              gsiDeclQAbstractTableModel.cc 1140 0x7fffefb38018 
24 QObject::~QObject                                                                                                                                          qobject.cpp                   1022 0x7fffe653d58b 
25 QItemSelectionModel::~QItemSelectionModel                                                                                                                  qitemselectionmodel.cpp       1175 0x7fffe64c4e0d 
26 QObjectPrivate::deleteChildren                                                                                                                             qobject.cpp                   2017 0x7fffe6532eee 
27 QWidget::~QWidget                                                                                                                                          qwidget.cpp                   1696 0x7fffe76b8c29 
28 QTableView_Adaptor::~QTableView_Adaptor                                                                                                                    gsiDeclQTableView.cc          2861 0x7fffeb21dd6b 
29 QTableView_Adaptor::~QTableView_Adaptor                                                                                                                    gsiDeclQTableView.cc          2861 0x7fffeb21dddc 
30 QObjectPrivate::deleteChildren                                                                                                                             qobject.cpp                   2017 0x7fffe6532eee 
31 QWidget::~QWidget                                                                                                                                          qwidget.cpp                   1696 0x7fffe76b8c29 
32 QMainWindow_Adaptor::~QMainWindow_Adaptor                                                                                                                  gsiDeclQMainWindow.cc         2066 0x7fffeabde609 
33 QMainWindow_Adaptor::~QMainWindow_Adaptor                                                                                                                  gsiDeclQMainWindow.cc         2066 0x7fffeabde66c 
34 gsi::_destroy<QMainWindow_Adaptor, true>::call                                                                                                             gsiClass.h                    292  0x7fffeabfde9b 
35 gsi::Class<QMainWindow_Adaptor, gsi::NoAdaptorTag>::destroy                                                                                                gsiClass.h                    588  0x7fffeabfd2f1 
36 pya::PYAObjectBase::~PYAObjectBase                                                                                                                         pyaObject.cc                  209  0x7fffef193591 
37 pya::pya_object_deallocate                                                                                                                                 pyaModule.cc                  636  0x7fffef1a50e2 
38 subtype_dealloc                                                                                                                                            typeobject.c                  1292 0x7ffff7accd43 
39 _Py_Dealloc                                                                                                                                                object.c                      2215 0x7ffff7ab1c96 
40 _Py_DECREF                                                                                                                                                 object.h                      478  0x7ffff7a9d795 
41 _Py_XDECREF                                                                                                                                                object.h                      541  0x7ffff7a9d795 
42 insertdict                                                                                                                                                 dictobject.c                  1102 0x7ffff7a9d795 
43 PyDict_SetItem                                                                                                                                             dictobject.c                  1545 0x7ffff7a9e75a 
44 _PyEval_EvalFrameDefault                                                                                                                                   ceval.c                       2292 0x7ffff7b6367c 
45 PyEval_EvalFrameEx                                                                                                                                         ceval.c                       741  0x7ffff7b5bd1d 
46 _PyEval_EvalCodeWithName                                                                                                                                   ceval.c                       4298 0x7ffff7b5c931 
47 PyEval_EvalCodeEx                                                                                                                                          ceval.c                       4327 0x7ffff7b5cabb 
48 PyEval_EvalCode                                                                                                                                            ceval.c                       718  0x7ffff7b5caed 
49 pya::PythonInterpreter::eval_string                                                                                                                        pya.cc                        499  0x7fffef15912d 
50 lym::Macro::run                                                                                                                                            lymMacro.cc                   1066 0x7ffff40f90a1 
51 lay::MacroEditorDialog::run                                                                                                                                layMacroEditorDialog.cc       3606 0x7ffff22b985a 
52 lay::MacroEditorDialog::run_this_button_clicked                                                                                                            layMacroEditorDialog.cc       3529 0x7ffff22b9d8e 
53 lay::MacroEditorDialog::qt_static_metacall                                                                                                                 moc_layMacroEditorDialog.cpp  316  0x7ffff24af9a3 
54 QMetaObject::activate                                                                                                                                      qobject.cpp                   3804 0x7fffe65351d0 
55 QAbstractButton::clicked                                                                                                                                   moc_qabstractbutton.cpp       312  0x7fffe776f806 
56 QAbstractButtonPrivate::emitClicked                                                                                                                        qabstractbutton.cpp           414  0x7fffe776fa2e 
57 QAbstractButtonPrivate::click                                                                                                                              qabstractbutton.cpp           407  0x7fffe7770e73 
58 QAbstractButton::mouseReleaseEvent                                                                                                                         qabstractbutton.cpp           1011 0x7fffe7771035 
59 QToolButton::mouseReleaseEvent                                                                                                                             qtoolbutton.cpp               622  0x7fffe78645fe 
60 QWidget::event                                                                                                                                             qwidget.cpp                   8981 0x7fffe76bd2b6 
61 QAbstractButton::event                                                                                                                                     qabstractbutton.cpp           968  0x7fffe7772273 
62 QToolButton::event                                                                                                                                         qtoolbutton.cpp               1001 0x7fffe78646a8 
63 QApplicationPrivate::notify_helper                                                                                                                         qapplication.cpp              3700 0x7fffe767aa66 
64 QApplication::notify                                                                                                                                       qapplication.cpp              3160 0x7fffe7684343 
65 lay::GuiApplication::notify                                                                                                                                layApplication.cc             1356 0x7ffff22271f9 
66 QCoreApplication::notifyInternal2                                                                                                                          qobject.h                     142  0x7fffe650980a 
67 QApplicationPrivate::sendMouseEvent                                                                                                                        qapplication.cpp              2646 0x7fffe7683457 
68 QWidgetWindow::handleMouseEvent                                                                                                                            atomic_base.h                 413  0x7fffe76d935d 
69 QWidgetWindow::event                                                                                                                                       qwidgetwindow.cpp             289  0x7fffe76dc1ec 
70 QWidgetWindow::event                                                                                                                                       qwidgetwindow.cpp             232  0x7fffe76dc1ec 
71 QApplicationPrivate::notify_helper                                                                                                                         qapplication.cpp              3700 0x7fffe767aa66 
72 QApplication::notify                                                                                                                                       qapplication.cpp              3446 0x7fffe76840f0 
73 lay::GuiApplication::notify                                                                                                                                layApplication.cc             1356 0x7ffff22271f9 
74 QCoreApplication::notifyInternal2                                                                                                                          qobject.h                     142  0x7fffe650980a 
75 QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent *)                                                                                                        0x7fffe6f327d3 
76 QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent *)                                                                                          0x7fffe6f3410b 
77 QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>)                                                                                                         0x7fffe6f0e35b 
78 ??                                                                                                                                                                                            0x7fffdf10c32e 
79 g_main_context_dispatch                                                                                                                                                                       0x7fffe534817d 
80 ??                                                                                                                                                                                            0x7fffe5348400 
81 g_main_context_iteration                                                                                                                                                                      0x7fffe53484a3 
82 QEventDispatcherGlib::processEvents                                                                                                                        qeventdispatcher_glib.cpp     422  0x7fffe6561435 
83 QEventLoop::exec                                                                                                                                           qflags.h                      140  0x7fffe65083ab 
84 QCoreApplication::exec                                                                                                                                     qflags.h                      120  0x7fffe6510116 
85 lay::GuiApplication::exec                                                                                                                                  layApplication.cc             1449 0x7ffff2227c08 
86 lay::ApplicationBase::run                                                                                                                                  layApplication.cc             1194 0x7ffff2226829 
87 klayout_main_cont                                                                                                                                          klayout.cc                    314  0x404726       
88 rba::run_app_func                                                                                                                                          rba.cc                        1907 0x7fffef2e1724 
89 ??                                                                                                                                                                                            0x7ffff787d026 
90 ??                                                                                                                                                                                            0x7ffff7889dab 
91 ??                                                                                                                                                                                            0x7ffff7870406 
92 ??                                                                                                                                                                                            0x7ffff78810ba 
93 rb_vm_exec                                                                                                                                                                                    0x7ffff7887870 
94 ??                                                                                                                                                                                            0x7ffff7718491 
95 ruby_run_node                                                                                                                                                                                 0x7ffff771d2ee 
96 rba::RubyInterpreter::initialize                                                                                                                           rba.cc                        2033 0x7fffef2e16a3 
97 klayout_main                                                                                                                                               klayout.cc                    244  0x403f77       
98 main                                                                                                                                                       klayout.cc                    144  0x403a07       

A solution might be to postpone the destruction using "deleteLater" rather than immediate delete.

A workaround is to explicitly delete the "wnd" window with "wnd._destroy()". This will execute the destructor code outside "Py_Dealloc".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions