| Summary: | With NULL callback and most buffer sizes, SDL_OpenAudioDevice allocates WAY more memory than intended | ||
|---|---|---|---|
| Product: | SDL | Reporter: | Solra Bizna <solrabizna> |
| Component: | audio | Assignee: | 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 | ||
> SDL_AUDIOBUFFERQUEUE_PACKETLEN additional queues will be allocated instead of the intended 1 additional queue.
:O
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. |
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.