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

Summary: With NULL callback and most buffer sizes, SDL_OpenAudioDevice allocates WAY more memory than intended
Product: SDL Reporter: Solra Bizna <solrabizna>
Component: audioAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: minor    
Priority: P2    
Version: 2.0.5   
Hardware: All   
OS: All   
Attachments: Patch that fixes the logic error

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.