Skip to content

media_reader: keep AudioBuffer size in step when resampling - #363

Merged
tedwaine merged 1 commit into
AcademySoftwareFoundation:developfrom
richardssam:fix/audio-buffer-recycling
Nov 20, 2026
Merged

tedwaine merged 1 commit into
AcademySoftwareFoundation:developfrom
richardssam:fix/audio-buffer-recycling

Conversation

@richardssam

Copy link
Copy Markdown
Contributor

Summarize your change.

When a source's audio is stretched or squashed to fit its frame rate, typed_resample() gives the AudioBuffer new, differently sized memory through Buffer::set_buf_data(). That call didn't update size_, so the buffer still reported its old size afterwards. As a result:

  • When the buffer was destroyed, the smaller block went back to the buffer recycler under the old, larger size, and the next allocation of that size wrote past its end.
  • allocate(), resize(), extend_size() and extend() also sized themselves from the stale value.

set_buf_data() now takes the size of the new buffer. typed_resample() also now handles two edge cases it got wrong before: a zero-length result, and an empty input.

Describe the reason for the change.

The stale size corrupted the heap during normal playback whenever audio was resampled. The crashes came later and somewhere else: freeing entries in GlobalAudioCacheActor, or malloc aborting with "memory corruption of free block". Running under macOS Guard Malloc caught the bad write, which led to typed_resample().

Describe what you have tested and on which operating system.

macOS 26.6 on an M2 Max.

  • New unit tests, both passing with the fix:
    • AudioBuffer.ResampleUpdatesSize checks size() after squashing, after stretching, and after extend().
    • AudioBuffer.ResampleEmpty covers a zero-length result and an empty input.
  • In the app, under Guard Malloc, playing a 4K ProRes clip with audio. Before the fix it aborted in typed_resample() while freeing a buffer that had been written past. After the fix it played without any Guard Malloc errors.
  • Not tested: Linux and Windows. The change is platform-neutral.

Add a list of changes, and note any that might need special attention during the review.

  • include/xstudio/media_reader/buffer.hpp: set_buf_data() now takes the size of the new buffer and updates size_. ⚠️ The signature has changed; typed_resample() is the only caller in the repo.
  • src/media_reader/src/media_reader.cpp:
    • typed_resample() passes the new size.
    • It returns a zeroed buffer when the result has zero samples. Before, int n = out_samples - 1; while (n--) ran about 2^32 times, writing past the buffer.
    • It also returns a zeroed buffer when the input is empty, where it used to read before the start of the buffer.
  • src/media_reader/test/media_reader_test.cpp: the two new AudioBuffer tests.

If possible, provide screenshots.

N/A

Developed with assistance from Claude.

typed_resample() swaps in a newly allocated sample buffer through
Buffer::set_buf_data(), which didn't update size_. After squashing a
buffer (e.g. when a source's rate is overridden, so its audio is
resampled to fit) size() still reported the old, larger size, so:

- ~Buffer handed the smaller block to the recycler under the larger
  size, and the next allocation of that size wrote past its end;
- allocate(), resize(), extend_size() and extend() sized from it.

This corrupted the heap during playback, showing up later as crashes
in the audio cache and in malloc.

set_buf_data() now takes the size of the new buffer. typed_resample()
also handles a zero-length result, which looped ~2^32 times writing
past the buffer, and an empty input, which was read before its start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Sam.Richards@taurich.org <Sam.Richards@taurich.org>
@richardssam
richardssam force-pushed the fix/audio-buffer-recycling branch from a7b19ee to 46dc958 Compare November 18, 2026 14:47
@tedwaine
tedwaine merged commit e7c1e40 into AcademySoftwareFoundation:develop Nov 20, 2026
3 of 4 checks passed
@tedwaine

Copy link
Copy Markdown
Collaborator

A nasty bug. thanks.

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.

2 participants