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

Fix QFile close bug with libsndfile #7171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/core/SampleDecoder.cpp
Expand Up @@ -62,18 +62,16 @@ auto decodeSampleSF(const QString& audioFile) -> std::optional<SampleDecoder::Re
SNDFILE* sndFile = nullptr;
auto sfInfo = SF_INFO{};

// TODO: Remove use of QFile
auto file = QFile{audioFile};
if (!file.open(QIODevice::ReadOnly)) { return std::nullopt; }

sndFile = sf_open_fd(file.handle(), SFM_READ, &sfInfo, false);
if (sf_error(sndFile) != 0) { return std::nullopt; }
sndFile = sf_open(audioFile.toStdString().c_str(), SFM_READ, &sfInfo);
if (sf_error(sndFile)) {
sf_close(sndFile);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per the libsndfile API:

Every call to sf_open_fd() should be matched with a call to sf_close() to free up memory allocated during the call to sf_open_fd().

We missed this one.

return std::nullopt;
}

auto buf = std::vector<sample_t>(sfInfo.channels * sfInfo.frames);
sf_read_float(sndFile, buf.data(), buf.size());

sf_close(sndFile);
file.close();

auto result = std::vector<sampleFrame>(sfInfo.frames);
for (int i = 0; i < static_cast<int>(result.size()); ++i)
Expand Down