Skip to content

Delete embedded widgets safely in model destructors (QPointer) - #547

Merged
paceholder merged 1 commit into
masterfrom
fix/embedded-widget-ownership
Nov 22, 2026
Merged

paceholder merged 1 commit into
masterfrom
fix/embedded-widget-ownership

Conversation

@paceholder

@paceholder paceholder commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation/refactoring

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. registerModel builds a temporary model to read its name(), and that model's widget is never embedded.

The fix has three parts. They do different jobs:

  • delete _widget in the destructor frees the leak. It frees a widget that was never embedded.
  • QPointer makes that delete safe. After embedding, the node's QGraphicsProxyWidget owns the widget and may delete it first. QPointer does not delete anything. It follows the QObject and becomes null when the object is destroyed, so the later delete does nothing. With a raw pointer the widget is deleted twice, which crashes when the scene is destroyed before the model.
  • Creating the widget lazily in embeddedWidget() means the temporary model makes no widget at all.

What changes:

  • All examples that hold a widget use a QPointer member and delete it in the destructor (calculator, text, resizable_images, node_validation).
  • docs/guide/data-flow.rst has a "Widget ownership" section with these rules.
  • New tests ([widget-ownership]) cover the temporary registry model, delete, undo and redo of the delete, and scene teardown before the model.

Only resizable_images and 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

  • Qt version tested: Qt 6.12.0, macOS (Apple Clang)
  • Existing tests still pass (731 assertions in 64 test cases, offscreen). The examples build.
  • Added tests for new functionality. They fail with both old patterns: a raw pointer without delete (the leak, a failed check) and a raw pointer with delete (SIGSEGV at scene teardown).
  • The leak is confirmed with heap and malloc_history (MallocStackLogging) on macOS: a live QLineEdit allocated in registerModel. leaks and AddressSanitizer do not report it, because Qt's widget list still references the widget. After the change, heap shows no live widget.
  • Not tested: Qt 5, Windows, Linux. The docs were not built (the new subsection uses ~~~ underlines, as styling.rst does).

Breaking changes?

No. Examples, docs and tests only.

Related issue

Fixes #248

@paceholder
paceholder force-pushed the fix/embedded-widget-ownership branch from 2f8e92a to 046320c Compare November 22, 2026 21:03
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
paceholder force-pushed the fix/embedded-widget-ownership branch from 046320c to 1e547da Compare November 22, 2026 21:06
@paceholder paceholder changed the title Show how to own an embedded widget safely: QPointer in examples and docs Delete embedded widgets safely in model destructors (QPointer) Nov 22, 2026
@paceholder
paceholder merged commit d77db36 into master Nov 22, 2026
3 checks passed
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.

A possible leaking memory problem

1 participant