# HG changeset patch # User Chris Cannam # Date 1727193221 -3600 # Tue Sep 24 16:53:41 2024 +0100 # Node ID ae6b35bdcb0c89335329be19a45396bb86107668 # Parent d3c5aac6b309a6d18d5b88b8a7ed3d584009b7e5 Avoid memory leak in buffer reallocation diff --git a/src/OggVorbisReadStream.cpp b/src/OggVorbisReadStream.cpp --- a/src/OggVorbisReadStream.cpp +++ b/src/OggVorbisReadStream.cpp @@ -42,10 +42,6 @@ #include #include -#ifndef __GNUC__ -#include -#endif - namespace breakfastquay { @@ -110,7 +106,10 @@ void readNextBlock() { if (m_finished) return; - if (oggz_read(m_oggz, 1024) <= 0) { + static const int blocksize = 8192; + long rv = oggz_read(m_oggz, blocksize); +// fprintf(stderr, "ogg: read=%ld\n", rv); + if (rv <= 0) { m_finished = true; } } @@ -120,7 +119,9 @@ if (!m_buffer) { m_buffer = new RingBuffer(samples); } else if (m_buffer->getSize() < samples) { - m_buffer = m_buffer->resized(samples); + RingBuffer *oldBuffer = m_buffer; + m_buffer = oldBuffer->resized(samples); + delete oldBuffer; } } @@ -133,6 +134,10 @@ int acceptFrames(float **frames, long n) { + if (n <= 0) { + return 0; + } + if (!m_namesRead) { const FishSoundComment *c; c = fish_sound_comment_first_byname(m_fishSound, (char *)"TITLE"); @@ -235,6 +240,9 @@ } int available = m_d->getAvailableFrameCount(); + +// fprintf(stderr, "ogg: requested=%d, available=%d\n", int(count), available); + if (size_t(available) < count) { count = available; }