Repository navigation
Delete embedded widgets safely in model destructors (QPointer) - #547
Merged
Merged
Conversation
paceholder
force-pushed
the
fix/embedded-widget-ownership
branch
from
November 22, 2026 21:03
2f8e92a to
046320c
Compare
The registry builds a temporary model to read its name. A widget made in the constructor of that model was never embedded, so it leaked. Deleting the widget in the destructor frees it. After embedding, the node proxy owns the widget and may delete it first. A raw pointer would then be deleted twice. A QPointer is null after the widget is destroyed, so the destructor can always delete it. Update all examples that hold a widget, add the ownership rules to the data-flow guide and add a regression test. Fixes #248
paceholder
force-pushed
the
fix/embedded-widget-ownership
branch
from
November 22, 2026 21:06
046320c to
1e547da
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of change
Description
Show how to own an embedded widget safely, in the examples and in the guide. There is no library code change.
The problem (#248): a model that creates its widget in the constructor leaks it.
registerModelbuilds a temporary model to read itsname(), and that model's widget is never embedded.The fix has three parts. They do different jobs:
delete _widgetin the destructor frees the leak. It frees a widget that was never embedded.QPointermakes thatdeletesafe. After embedding, the node'sQGraphicsProxyWidgetowns the widget and may delete it first.QPointerdoes not delete anything. It follows theQObjectand becomes null when the object is destroyed, so the laterdeletedoes nothing. With a raw pointer the widget is deleted twice, which crashes when the scene is destroyed before the model.embeddedWidget()means the temporary model makes no widget at all.What changes:
QPointermember and delete it in the destructor (calculator, text, resizable_images, node_validation).docs/guide/data-flow.rsthas a "Widget ownership" section with these rules.[widget-ownership]) cover the temporary registry model, delete, undo and redo of the delete, and scene teardown before the model.Only
resizable_imagesand the text display model created their widget in the constructor and leaked. The other examples were already lazy. I changed them too, so the examples show one pattern.Testing
delete(the leak, a failed check) and a raw pointer withdelete(SIGSEGV at scene teardown).heapandmalloc_history(MallocStackLogging) on macOS: a liveQLineEditallocated inregisterModel.leaksand AddressSanitizer do not report it, because Qt's widget list still references the widget. After the change,heapshows no live widget.~~~underlines, asstyling.rstdoes).Breaking changes?
No. Examples, docs and tests only.
Related issue
Fixes #248