Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement ledger lines #24011

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

mike-spa
Copy link
Contributor

Resolves: a crash which happens in a specific file under specific conditions @oktophonie. The crash is due to a read-after-free so it doesn't always crash. It can be reliably reproduced only by using the adress sanitizer, which shows that this is yet another instance of our well known invalid ledger lines problem. Hopefully this is the end of that @cbjeukendrup @miiizen.

To summarize

Two things are bad about the current way we do ledger lines.

a) They are implemented as a linked list, with the Chord holding the pointer to the first LedgerLine of the list and each LedgerLine holding a pointer to the next. This makes zero sense: it makes the list slower to traverse, it wastes memory, and it makes the code uglier for no reason. I've now reimplemented it with the Chord holding a vector of LedgerLines* instead.

b) They are deleted and recreated at every single layout. This is a waste of resources, as we keep deleting and reallocating things in the heap for no reason, but most importanly has been the source of countless (meaning that I've literally lost count) memory bugs as anything that holds pointers to these LedgerLines (especially Shapes and now Skylines too) get invalidated. Now ledger lines are deleted / created only if necessary, i.e. only if the total number of ledger lines needed by the chord has changed. This should be, hopefully, the end of those memory bugs.

Two things are bad about the current way we do ledger lines.

a) They are implemented as a linked list, with the Chord holding the pointer to the first LedgerLine of the list and each LedgerLine holding a pointer to the next. This makes zero sense: it makes the list slower to traverse, it wastes memory, and it makes the code uglier for no reason. I've now reimplemented it with the Chord holding a vector of LedgerLines* instead.

b) They are deleted and recreated at every single layout. This is a waste of resources, as we keep deleting and reallocating things in the heap for no reason, but most importanly has been the source of countless (meaning that I've literally lost count) memory bugs as anything that holds pointers to these LedgerLines (especially Shapes and now Skylines too) get invalidated. Now ledger lines are deleted / created only if necessary, i.e. only if the total number of ledger lines needed by the chord has changed. This should be, hopefully, the end of those memory bugs.
@@ -437,8 +423,6 @@ void ChordLayout::layoutTablature(Chord* item, LayoutContext& ctx)
ldgLin->setVisible(item->visible());
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we're still allocating new ledger lines here. Probably needs the same changes as the dev version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, thanks!

@oktophonie oktophonie added crash Issues involving a crash of MuseScore P1 Priority: High labels Aug 14, 2024
@oktophonie oktophonie added the vtests This PR produces approved changes to vtest results label Aug 14, 2024
@RomanPudashkin RomanPudashkin merged commit 4f2b9e2 into musescore:master Aug 14, 2024
10 of 11 checks passed
RomanPudashkin added a commit to RomanPudashkin/MuseScore that referenced this pull request Aug 20, 2024
@RomanPudashkin RomanPudashkin mentioned this pull request Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Issues involving a crash of MuseScore P1 Priority: High vtests This PR produces approved changes to vtest results
Projects
Development

Successfully merging this pull request may close these issues.

4 participants