libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.37k stars 1.74k forks source link

[SDL2] [MacOS] CFStringRef memory leak in `assign_device_to_audioqueue` in SDL_coreaudio.m #9943

Open tadashibashi opened 3 months ago

tadashibashi commented 3 months ago

I just ran a customary leak check and found that there is a CFStringRef leaking in assign_device_to_audioqueue in SDL_coreaudio.m. Version is SDL 2.30.3

I'm not familiar with the core audio api, but it seems like AudioQueueSetProperty makes a copy of the string, so it's safe to CFBridgingRelease(devuid);? It fixes the leak, but wanted to ask before submitting a pull request.

Here's the code for quick reference

static int assign_device_to_audioqueue(_THIS)
{
    const AudioObjectPropertyAddress prop = {
        kAudioDevicePropertyDeviceUID,
        this->iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
        kAudioObjectPropertyElementMain
    };

    OSStatus result;
    CFStringRef devuid;
    UInt32 devuidsize = sizeof(devuid);
    result = AudioObjectGetPropertyData(this->hidden->deviceID, &prop, 0, NULL, &devuidsize, &devuid);
    CHECK_RESULT("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID)");
    result = AudioQueueSetProperty(this->hidden->audioQueue, kAudioQueueProperty_CurrentDevice, &devuid, devuidsize);
    CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)");

    CFBridgingRelease(devuid); // added here
    return 1;
}
icculus commented 3 months ago

Guessing this is still an issue in SDL3, so putting it there so I am more likely to look at this soon.