diff --git a/docs/README-migration.md b/docs/README-migration.md index 0fc2b412a2..29c4294678 100644 --- a/docs/README-migration.md +++ b/docs/README-migration.md @@ -1564,7 +1564,7 @@ Rather than iterating over sensors using device index, there is a new function S { if (SDL_InitSubSystem(SDL_INIT_SENSOR) == 0) { int i, num_sensors; - SDL_SensorID *sensors = SDL_GetSensors(&num_sensors); + const SDL_SensorID *sensors = SDL_GetSensors(&num_sensors); if (sensors) { for (i = 0; i < num_sensors; ++i) { SDL_Log("Sensor %" SDL_PRIu32 ": %s, type %d, platform type %d\n", @@ -1573,7 +1573,6 @@ Rather than iterating over sensors using device index, there is a new function S SDL_GetSensorTypeForID(sensors[i]), SDL_GetSensorNonPortableTypeForID(sensors[i])); } - SDL_free(sensors); } SDL_QuitSubSystem(SDL_INIT_SENSOR); } diff --git a/include/SDL3/SDL_sensor.h b/include/SDL3/SDL_sensor.h index dc43782f1a..2743f3734b 100644 --- a/include/SDL3/SDL_sensor.h +++ b/include/SDL3/SDL_sensor.h @@ -146,14 +146,13 @@ typedef enum SDL_SensorType /** * Get a list of currently connected sensors. * - * \param count a pointer filled in with the number of sensors returned. - * \returns a 0 terminated array of sensor instance IDs which should be freed - * with SDL_free(), or NULL on failure; call SDL_GetError() for more + * \param count a pointer filled in with the number of sensors returned, may be NULL. + * \returns a 0 terminated array of sensor instance IDs or NULL on failure; call SDL_GetError() for more * information. * * \since This function is available since SDL 3.0.0. */ -extern SDL_DECLSPEC SDL_SensorID * SDLCALL SDL_GetSensors(int *count); +extern SDL_DECLSPEC const SDL_SensorID * SDLCALL SDL_GetSensors(int *count); /** * Get the implementation dependent name of a sensor. diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index e826ed4579..64b0d0a4a5 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -470,7 +470,7 @@ SDL_DYNAPI_PROC(int,SDL_GetSensorNonPortableTypeForID,(SDL_SensorID a),(a),retur SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetSensorProperties,(SDL_Sensor *a),(a),return) SDL_DYNAPI_PROC(SDL_SensorType,SDL_GetSensorType,(SDL_Sensor *a),(a),return) SDL_DYNAPI_PROC(SDL_SensorType,SDL_GetSensorTypeForID,(SDL_SensorID a),(a),return) -SDL_DYNAPI_PROC(SDL_SensorID*,SDL_GetSensors,(int *a),(a),return) +SDL_DYNAPI_PROC(const SDL_SensorID*,SDL_GetSensors,(int *a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetSilenceValueForFormat,(SDL_AudioFormat a),(a),return) SDL_DYNAPI_PROC(int,SDL_GetStorageFileSize,(SDL_Storage *a, const char *b, Uint64 *c),(a,b,c),return) SDL_DYNAPI_PROC(int,SDL_GetStoragePathInfo,(SDL_Storage *a, const char *b, SDL_PathInfo *c),(a,b,c),return) diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c index d2523d6bea..239fb02ca0 100644 --- a/src/joystick/SDL_joystick.c +++ b/src/joystick/SDL_joystick.c @@ -858,7 +858,7 @@ static SDL_bool IsROGAlly(SDL_Joystick *joystick) SDL_bool has_ally_gyro = SDL_FALSE; if (SDL_InitSubSystem(SDL_INIT_SENSOR) == 0) { - SDL_SensorID *sensors = SDL_GetSensors(NULL); + const SDL_SensorID *sensors = SDL_GetSensors(NULL); if (sensors) { int i; for (i = 0; sensors[i]; ++i) { @@ -877,7 +877,6 @@ static SDL_bool IsROGAlly(SDL_Joystick *joystick) } } } - SDL_free(sensors); } SDL_QuitSubSystem(SDL_INIT_SENSOR); } @@ -952,7 +951,7 @@ static SDL_bool ShouldAttemptSensorFusion(SDL_Joystick *joystick, SDL_bool *inve static void AttemptSensorFusion(SDL_Joystick *joystick, SDL_bool invert_sensors) { - SDL_SensorID *sensors; + const SDL_SensorID *sensors; unsigned int i, j; SDL_AssertJoysticksLocked(); @@ -981,7 +980,6 @@ static void AttemptSensorFusion(SDL_Joystick *joystick, SDL_bool invert_sensors) SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f); } } - SDL_free(sensors); } SDL_QuitSubSystem(SDL_INIT_SENSOR); diff --git a/src/sensor/SDL_sensor.c b/src/sensor/SDL_sensor.c index f79debe372..466bf3734c 100644 --- a/src/sensor/SDL_sensor.c +++ b/src/sensor/SDL_sensor.c @@ -170,7 +170,7 @@ SDL_bool SDL_SensorsOpened(void) return opened; } -SDL_SensorID *SDL_GetSensors(int *count) +const SDL_SensorID *SDL_GetSensors(int *count) { int i, num_sensors, device_index; int sensor_index = 0, total_sensors = 0; @@ -207,7 +207,7 @@ SDL_SensorID *SDL_GetSensors(int *count) } SDL_UnlockSensors(); - return sensors; + return SDL_FreeLater(sensors); } /* diff --git a/test/testsensor.c b/test/testsensor.c index 404c9723bf..38bced06ea 100644 --- a/test/testsensor.c +++ b/test/testsensor.c @@ -58,7 +58,7 @@ static void HandleSensorEvent(SDL_SensorEvent *event) int main(int argc, char **argv) { - SDL_SensorID *sensors; + const SDL_SensorID *sensors; int i, num_sensors, num_opened; SDLTest_CommonState *state; @@ -104,7 +104,6 @@ int main(int argc, char **argv) } } } - SDL_free(sensors); } SDL_Log("Opened %d sensors\n", num_opened);