Skip to content

Fix stale pointer entries in EID register when items are deleted#34170

Open
manolo wants to merge 3 commits into
musescore:mainfrom
manolo:fix/eid-register-stale-pointers
Open

Fix stale pointer entries in EID register when items are deleted#34170
manolo wants to merge 3 commits into
musescore:mainfrom
manolo:fix/eid-register-stale-pointers

Conversation

@manolo

@manolo manolo commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Resolves: #34167

The EID register maps raw pointers, and deleting an item did not remove its entry. A later allocation could reuse the freed address and collide with the stale entry, corrupting the register and asserting in Debug builds when elements are read in paste mode. See the issue for the full analysis.

The fix unregisters items in Score::onElementDestruction, which already guards against scores under destruction. The two new regression tests fail deterministically on all platforms without the fix, making the problem visible in CI even though the crash itself only manifested on macOS.

  • I signed the CLA
  • The title of the PR describes the problem it addresses.
  • Each commit's message describes its purpose and effects, and references the issue it resolves.
  • The code in the PR follows the coding rules.
  • I understand all aspects of the code I'm contributing and I'm able to explain it if requested.
  • The code compiles and runs on my machine, preferably after each commit individually. I have manually tested and verified that my changes fulfil their intended purpose.
  • No prior attempts to resolve this problem exist, or if they do, I listed them in my PR description and described how I avoided repeating past mistakes.
  • There are no unnecessary changes.
  • I created a unit test or vtest to verify the changes I made (if applicable).

Deleting an item left its entry in the EID register, which maps raw
pointers. A later allocation could reuse the freed address and collide
with the stale entry, corrupting the register and asserting in Debug
builds when elements are read in paste mode. Unregister items in
Score::onElementDestruction, which already guards against scores under
destruction via validScores.

Fixes musescore#34167
@coderabbitai

coderabbitai Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a89514ed-6854-45ad-becd-a3b6c90b6345

📥 Commits

Reviewing files that changed from the base of the PR and between 4bb00e2 and d46510d.

📒 Files selected for processing (1)
  • src/engraving/tests/eid_tests.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/engraving/tests/eid_tests.cpp

📝 Walkthrough

Walkthrough

Added EID register cleanup for destroyed engraving objects, removing both mapping directions. Score destruction now notifies the master score’s register, with validity checks for the master and current scores. Tests cover dynamic element deletion, excerpt cleanup, and clipboard-style element round trips.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: removing stale EID register entries when items are deleted.
Description check ✅ Passed The description covers the issue, fix, tests, and checklist items, with only minor template details like a username left unspecified.
Linked Issues check ✅ Passed The PR implements the linked issue's fix by unregistering deleted elements from EIDRegister and adds regression tests for the reported crash.
Out of Scope Changes check ✅ Passed The code changes are focused on the stale EID entry bug and its tests, with no clear unrelated or unnecessary additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Linked repositories: Public OSS repositories can only analyze public repositories installed in this organization. No linked repositories were analyzed; skipped musescore/muse_framework.git.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/engraving/dom/score.cpp (1)

375-393: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Cleanup is skipped during score teardown. Score::~Score() removes the score from validScores before deleting its items, so onElementDestruction() returns early for teardown-owned elements. That leaves the master score's EIDRegister stale when an excerpt score is deleted via Excerpt::~Excerpt() / MasterScore::removeExcerpt().

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/engraving/dom/score.cpp` around lines 375 - 393, Update
Score::onElementDestruction so score teardown still unregisters destroyed
elements from the master score's EIDRegister even after the score is removed
from validScores. Preserve the early return for null or unrelated scores, while
ensuring excerpt-owned elements deleted through Excerpt::~Excerpt() or
MasterScore::removeExcerpt() reach eidRegister()->onItemDestroyed(e).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/engraving/dom/score.cpp`:
- Around line 375-393: Update Score::onElementDestruction so score teardown
still unregisters destroyed elements from the master score's EIDRegister even
after the score is removed from validScores. Preserve the early return for null
or unrelated scores, while ensuring excerpt-owned elements deleted through
Excerpt::~Excerpt() or MasterScore::removeExcerpt() reach
eidRegister()->onItemDestroyed(e).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: eef229fd-a5ac-4c72-a732-e60bbebaad65

📥 Commits

Reviewing files that changed from the base of the PR and between 83bc55b and 87804b4.

📒 Files selected for processing (4)
  • src/engraving/dom/score.cpp
  • src/engraving/infrastructure/eidregister.cpp
  • src/engraving/infrastructure/eidregister.h
  • src/engraving/tests/eid_tests.cpp

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/engraving/tests/eid_tests.cpp`:
- Line 98: Replace the post-delete assertion using EIDFromItem(measure) with an
itemFromEID(eid) lookup, asserting that the returned item is invalid or absent.
Update the assertion in the relevant EID test while preserving the existing
expectation that the deleted measure’s EID no longer resolves.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f2507fb8-5960-4303-bf11-0f139daecde7

📥 Commits

Reviewing files that changed from the base of the PR and between 87804b4 and 4bb00e2.

📒 Files selected for processing (2)
  • src/engraving/dom/score.cpp
  • src/engraving/tests/eid_tests.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/engraving/dom/score.cpp

Comment thread src/engraving/tests/eid_tests.cpp Outdated
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.

Unit tests crash on macOS Debug builds: assert in EIDRegister::registerItemEID due to stale pointer entries

2 participants