We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3685 - With NULL callback and most buffer sizes, SDL_OpenAudioDevice allocates WAY more memory than intended
Summary: With NULL callback and most buffer sizes, SDL_OpenAudioDevice allocates WAY m...
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: audio (show other bugs)
Version: 2.0.5
Hardware: All All
: P2 minor
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-06-27 22:27 UTC by Solra Bizna
Modified: 2017-06-27 22:49 UTC (History)
0 users

See Also:


Attachments
Patch that fixes the logic error (840 bytes, patch)
2017-06-27 22:27 UTC, Solra Bizna
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Solra Bizna 2017-06-27 22:27:48 UTC
Created attachment 2779 [details]
Patch that fixes the logic error

With a NULL callback, SDL_OpenAudioDevice sets up the device to be fed with SDL_QueueAudio. It attempts to allocate at least enough SDL_AudioBufferQueues to store one buffer of audio data. Unfortunately, due to a small logic error, if the buffer size is not an exact multiple of SDL_AUDIOBUFFERQUEUE_PACKETLEN, SDL_AUDIOBUFFERQUEUE_PACKETLEN additional queues will be allocated instead of the intended 1 additional queue. In my program, this resulted in the allocation of 67MB worth of unnecessary queues, and was singlehandedly responsible for 90% of the program's total memory usage.

With the attached patch, the extra memory usage is gone and my program still works. A few cursory attempts to exercise any broken code paths that may have been unearthed by the patch bore no fruit.

Severity is minor because this bug does not cause crashes, security risks, glitches, or broken functionality. The only visible effect is the extra memory usage, which would normally only be a problem on embedded platforms and Emscripten.
Comment 1 Ryan C. Gordon 2017-06-27 22:39:37 UTC
> SDL_AUDIOBUFFERQUEUE_PACKETLEN additional queues will be allocated instead of the intended 1 additional queue.

:O
Comment 2 Ryan C. Gordon 2017-06-27 22:49:56 UTC
This is a good catch, and I wasn't aware of the bug, but the good news is we already replaced this code when we added a generic SDL_DataQueue API here:

https://hg.libsdl.org/SDL/rev/34b25c97b17f

At first glance, it doesn't appear that SDL_DataQueue inherited this bug, so I'm marking this bug fixed.

--ryan.