Skip to content

Fix PRINT_REDIRECTOR calling into a destroyed LogViewer#593

Open
markomarkovic wants to merge 1 commit into
CadQuery:masterfrom
markomarkovic:logviewer-print-redirector
Open

Fix PRINT_REDIRECTOR calling into a destroyed LogViewer#593
markomarkovic wants to merge 1 commit into
CadQuery:masterfrom
markomarkovic:logviewer-print-redirector

Conversation

@markomarkovic

Copy link
Copy Markdown

Summary

PRINT_REDIRECTOR (a module-level singleton in main_window.py) connected a lambda to sigStdoutWrite for each MainWindow. The lambda was never disconnected, closed over self, and resolved self.components["log"] late through the dict MainMixin holds as a class attribute — so it reached whichever LogViewer registered last. Once a LogViewer was destroyed, the next print() called into a deleted C++ object and raised RuntimeError: wrapped C/C++ object of type LogViewer has been deleted.

Connecting the bound method self.components["log"].append instead lets PyQt drop the connection automatically when the LogViewer is destroyed.

Testing

Adds test_print_redirector_released_with_window: destroys a window's LogViewer, emits on the singleton, and asserts no exception reaches sys.excepthook. It fails on the old lambda and passes with the fix.

pytest tests/ — full suite passes.

The latent bug exists on master today; it just isn't exercised until multiple MainWindows are created and torn down (as the display-modes tests do).

Claude AI found and fixed the issue.

PRINT_REDIRECTOR is a module-level singleton, so the lambda connected to
sigStdoutWrite in prepare_panes accumulated one connection per MainWindow and
was never disconnected. Each lambda also closed over self and resolved
self.components["log"] late through a dict that MainMixin holds as a class
attribute, so it reached whichever LogViewer registered last. Once a LogViewer
was destroyed, the next print() called into a deleted C++ object and raised
RuntimeError.

Connecting the bound method instead lets PyQt drop the connection when the
receiver is destroyed.
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