-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Fix #15925: Include lyrics in MIDI export #21541
base: master
Are you sure you want to change the base?
Conversation
Could this one get reviewed please? |
for (const auto& lyric : cr->lyrics()) { | ||
ByteArray lyricText = lyric->plainText().toUtf8(); | ||
size_t len = lyricText.size() + 1; | ||
unsigned char* data = new unsigned char[len]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like memory leak here. According to the fact that this code used in the loop there is huge memory leak possible =)
Isn't there a method in mu::String
which returns char*
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just delete[] data;
at the end of that loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this method is full of similar memory leaks, therefore I ignored it while reviewing.
Isn't there a method in mu::String which returns char*?
No, because mu::String uses utf16 so if you want char* you need to convert.
Or just
delete[] data;
at the end of that loop
Hell no 😄 because we're storing a pointer to it in the resulting MidiEvent.
I think the conclusion is that we should refactor to avoid the need for char*, and store it differently in MidiEvent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t len = lyricText.size(); // Not sure + 1 is necessary. If so it should be data[len] = '\0', otherwise it's undefined
However why not to use easier approach without unnecessary memory allocation?
std::string lyricText = lyric->plainText().toStdString();
size_t len = lyricText.size();
ev.setEData(lyricText.c_str());
ev.setLen(static_cast<int>(len));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hell no 😄 because we're storing a pointer to it in the resulting MidiEvent.
Good point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ev.setEData(lyricText.c_str());
c_str()
is not an unsigned char *
like setEData()
expects, seems some casting is needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But even with that it just doesn't work, on importing such a MIDI the lyrics are completly broken
Actually always the same syllable, but even that sometimes trunccated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t len = lyricText.size(); // Not sure + 1 is necessary. If so it should be data[len] = '\0', otherwise it's undefined
However why not to use easier approach without unnecessary memory allocation?
std::string lyricText = lyric->plainText().toStdString(); size_t len = lyricText.size(); ev.setEData(lyricText.c_str()); ev.setLen(static_cast<int>(len));
That's basically equivalent to using delete
: as soon as lyricText
goes out of scope, the underlying c string is deleted but the MidiEvent still contains a pointer to it. That's probably why the result is "completely broken" on Windows and would cause Address Sanitizer complaints on macOS/Linux.
Resolves: #15925