mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-16 14:56:00 +00:00
Introduce formal policy for APIs that return strings.
This declares that any `const char *` returned from SDL is owned by SDL, and promises to be valid _at least_ until the next time the event queue runs, or SDL_Quit() is called, even if the thing that owns the string gets destroyed or changed before then. This is noted in the headers as "the SDL_GetStringRule", so this will both be greppable to find a detailed explaination in docs/README-strings.md and wikiheaders will automatically turn it into a link we can point at the appropriate documentation. Fixes #9902. (and several FIXMEs, both known and yet-undocumented.)
This commit is contained in:
@@ -131,6 +131,7 @@ int SDL_GetNumAudioDrivers(void)
|
||||
return num_drivers;
|
||||
}
|
||||
|
||||
// this returns string literals, so there's no need to use SDL_FreeLater.
|
||||
const char *SDL_GetAudioDriver(int index)
|
||||
{
|
||||
if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
|
||||
@@ -139,6 +140,7 @@ const char *SDL_GetAudioDriver(int index)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// this returns string literals, so there's no need to use SDL_FreeLater.
|
||||
const char *SDL_GetCurrentAudioDriver(void)
|
||||
{
|
||||
return current_audio.name;
|
||||
@@ -521,7 +523,7 @@ static void DestroyPhysicalAudioDevice(SDL_AudioDevice *device)
|
||||
SDL_DestroyMutex(device->lock);
|
||||
SDL_DestroyCondition(device->close_cond);
|
||||
SDL_free(device->work_buffer);
|
||||
SDL_free(device->name);
|
||||
SDL_FreeLater(device->name); // this pointer is handed to the app during SDL_GetAudioDeviceName
|
||||
SDL_free(device);
|
||||
}
|
||||
|
||||
@@ -1402,12 +1404,12 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
|
||||
return SDL_FindPhysicalAudioDeviceByCallback(TestDeviceHandleCallback, handle);
|
||||
}
|
||||
|
||||
char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
|
||||
const char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
|
||||
{
|
||||
char *retval = NULL;
|
||||
const char *retval = NULL;
|
||||
SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid);
|
||||
if (device) {
|
||||
retval = SDL_strdup(device->name);
|
||||
retval = device->name;
|
||||
}
|
||||
ReleaseAudioDevice(device);
|
||||
|
||||
|
Reference in New Issue
Block a user