Skip to content

Fix memory leak in MIDI export #26415 #26798

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

Merged
merged 5 commits into from
Mar 1, 2025

Conversation

tstibor
Copy link
Contributor

@tstibor tstibor commented Feb 27, 2025

Replace new unsigned char allocations with std::vector and make sure that MidiEvent::setEData takes vector reference rather than raw pointer.

Resolves: #26415

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 27, 2025
Replace new unsigned char allocations with QVector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 27, 2025
Replace new unsigned char allocations with QVector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 27, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
@@ -587,9 +586,8 @@ bool MidiFile::readEvent(MidiEvent* event)
LOGD("readEvent: error 6");
Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Feb 28, 2025

Choose a reason for hiding this comment

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

Shouldn't/Couldn't the above if be an else if?

    if (me == 0xf0 || me == 0xf7) {
...
    } else if (me == ME_META) {
...
    }
    if (me & 0x80) {                       // status byte

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Jojo-Schmitz I think you are right and that should not introduce any issues.

if (me == 0xf0 || me == 0xf7) {
    // SysEx handling
    ...
    return ..
} else if (me == ME_META) {
    // Meta handling
    ...
    return ...
}
if (me & 0x80) { 
    ... 
}

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to be a small optimization

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably this small optimization can go into a separate PR

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Mar 1, 2025

Choose a reason for hiding this comment

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

Don't see why, but maybe into a separate commit of this PR

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Feb 28, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
@Jojo-Schmitz
Copy link
Contributor

Jojo-Schmitz commented Feb 28, 2025

I'm puzzled about the vtest failure:
leadingNonChordSeg-1 diff
Seems entirely unrelated, maybe a rebase helps?

Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 1, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
tstibor added 2 commits March 1, 2025 11:47
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.
Make sure the vector size is properly resized before
read operation and remove null termination.

Avoid copy assignment by using move semantics.
@tstibor tstibor force-pushed the fix_memleak_26415 branch from 04fdc89 to 05befe3 Compare March 1, 2025 10:49
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 1, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
@tstibor
Copy link
Contributor Author

tstibor commented Mar 1, 2025

I'm puzzled about the vtest failure: leadingNonChordSeg-1 diff leadingNonChordSeg-1 diff Seems entirely unrelated, maybe a rebase helps?

So rebased fixed it!

if (dataLen) {
read(data, dataLen);
data.resize(dataLen + 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems the resize needs to be donw before the if, at least in my backport I do get a crash in the unit tests otherwise, terminate called after throwing an instance of 'std::logic_error'

Copy link
Contributor

@Jojo-Schmitz Jojo-Schmitz Mar 1, 2025

Choose a reason for hiding this comment

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

Seems the 2nd time this PR reveals an issue with the (MIDI import) unit tests vs. those from Mu3

Backport by Jojo-Schmitz showed a 'std::logic_error'
when resizing data vector is inside the if-statement
rather than outside the if-statement.
Jojo-Schmitz pushed a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 1, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798
Copy link
Member

@cbjeukendrup cbjeukendrup left a comment

Choose a reason for hiding this comment

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

Thanks, looks good to me now!

@cbjeukendrup cbjeukendrup merged commit fc109b8 into musescore:master Mar 1, 2025
11 checks passed
Jojo-Schmitz added a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 2, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798

Co-Authored-By: Casper Jeukendrup <[email protected]>
Co-Authored-By: Thomas Stibor <[email protected]>
@Jojo-Schmitz
Copy link
Contributor

Something for 4.5.0?

Jojo-Schmitz added a commit to Jojo-Schmitz/MuseScore that referenced this pull request Mar 2, 2025
Replace new unsigned char allocations with std::vector<unsigned char> and make sure that
MidiEvent::setEData takes vector reference rather than raw pointer.

Backport of musescore#26798

Co-Authored-By: Casper Jeukendrup <[email protected]>
Co-Authored-By: Thomas Stibor <[email protected]>
@cbjeukendrup
Copy link
Member

I'm not completely sure. Maybe better not, because this seems the type of change where an oversight is easily made, so it may be better to give it some time to ripe.

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.

Memory leak in MIDI export
3 participants