Skip to content

Release QLayout.takeAt QWidgetItems via shiboken6.delete and avoid QLayout.itemAt in QtFloatLayer - #2276

Merged
trissim merged 1 commit into
mainfrom
fix/shiboken-qwidgetitem-cleanup
Nov 24, 2026
Merged

trissim merged 1 commit into
mainfrom
fix/shiboken-qwidgetitem-cleanup

Conversation

@trissim

@trissim trissim commented Nov 24, 2026

Copy link
Copy Markdown

Root Cause

Observed an intermittent crash in shipped_run right after closing menus and opening a channel:

File "toad/qt/history.py", line 1293, in __init__
  _scroller = QScroller.scroller(self.viewport())
TypeError: 'PySide6.QtWidgets.QScroller.scroller' called with wrong argument types:
  PySide6.QtWidgets.QScroller.scroller(QWidgetItem)

Verified in Python against PySide6/shiboken6:

  • QLayout.takeAt(0) returns a non-QObject QWidgetItem with shiboken6.ownedByPython(item) == False, leaking the C++ QWidgetItem on every teardown (side_bar.py:330, menu.py:172, placement.py:141, interactive_dialog.py:155, question.py:142, composer.py:797, plan_panel.py:65).
  • QLayout.itemAt(i) in QtFloatLayer creates C++-owned QWidgetItem wrappers whose shiboken6.isValid() stays True even after QWidget.deleteLater() frees the C++ QWidgetItem (valid=200/200), leaving stale entries in shiboken6's BindingManager. When malloc later reuses that address for a new QWidget (such as QListView.viewport()), shiboken6 hands back the stale QWidgetItem wrapper.

Fix

  • Added shared drain_layout_items(layout, *, keep=0) and delete_layout_widgets(layout, *, keep=0) in src/toad/qt/metrics.py that call shiboken6.delete(item) immediately after layout.takeAt(0) (0 new cold-start modules; folds 7 duplicated takeAt(0) loops — IMPL-12).
  • Replaced all 3 self.notices.itemAt(i) loops in src/toad/qt/float_layer.py with self.findChildren(QtNoticeCard, options=Qt.FindChildOption.FindDirectChildrenOnly) + stale.setParent(None) before deleteLater() so QtFloatLayer never wraps a QWidgetItem.

Verification

  • Offscreen Qt test verifying delete_layout_widgets, drain_layout_items, shiboken6.delete stress test (500 items, 0 crashes), and QtFloatLayer eviction/order.
  • Cold start: PRE-SPLASH 253 / POST-LOAD 1758 (comms=False, pkg=False).
  • Guards: qt_inherit_ratchet.py = 0, shadow_check.py = 0, guarded_imports.py = 0, rel_imports = 0.

@trissim
trissim merged commit c3b4c70 into main Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant