Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leaks SDL_stack_alloc/SDL_stack_free #10574

Closed
0xc0de opened this issue Aug 22, 2024 · 4 comments
Closed

Memory leaks SDL_stack_alloc/SDL_stack_free #10574

0xc0de opened this issue Aug 22, 2024 · 4 comments
Milestone

Comments

@0xc0de
Copy link

0xc0de commented Aug 22, 2024

Not all calls to SDL_stack_alloc have a corresponding SDL_stack_free. This causes memory leaks if SDL_DISABLE_ALLOCA is defined.
(See RAWINPUT_JoystickOpen).

@slouken
Copy link
Collaborator

slouken commented Aug 22, 2024

Feel free to make a PR!

@slouken slouken added this to the 3.2.0 milestone Aug 22, 2024
@sezero
Copy link
Contributor

sezero commented Aug 24, 2024

If the issue is only in RAWINPUT_JoystickOpen, are the following OK?

For SDL3:

diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index 4b0213c..24422e7 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -1294,6 +1294,7 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
     value_caps = SDL_stack_alloc(HIDP_VALUE_CAPS, caps.NumberInputValueCaps);
     if (SDL_HidP_GetValueCaps(HidP_Input, value_caps, &caps.NumberInputValueCaps, ctx->preparsed_data) != HIDP_STATUS_SUCCESS) {
         RAWINPUT_JoystickClose(joystick);
+        SDL_stack_free(button_caps);
         return SDL_SetError("Couldn't get device value capabilities");
     }
 
@@ -1322,6 +1323,8 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         ctx->button_indices = (USHORT *)SDL_malloc(joystick->nbuttons * sizeof(*ctx->button_indices));
         if (!ctx->button_indices) {
             RAWINPUT_JoystickClose(joystick);
+            SDL_stack_free(value_caps);
+            SDL_stack_free(button_caps);
             return -1;
         }
 
@@ -1369,12 +1372,15 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         joystick->naxes += 1;
     }
 
+    SDL_stack_free(button_caps);
+
     if (joystick->naxes > 0) {
         int axis_index = 0;
 
         ctx->axis_indices = (USHORT *)SDL_malloc(joystick->naxes * sizeof(*ctx->axis_indices));
         if (!ctx->axis_indices) {
             RAWINPUT_JoystickClose(joystick);
+            SDL_stack_free(value_caps);
             return -1;
         }
 
@@ -1408,6 +1414,7 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         ctx->hat_indices = (USHORT *)SDL_malloc(joystick->nhats * sizeof(*ctx->hat_indices));
         if (!ctx->hat_indices) {
             RAWINPUT_JoystickClose(joystick);
+            SDL_stack_free(value_caps);
             return -1;
         }
 
@@ -1426,6 +1433,8 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         }
     }
 
+    SDL_stack_free(value_caps);
+
 #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
     if (ctx->is_xinput) {
         SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);

For SDL2:

diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index 2e10708..9f9d3f2 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -1290,6 +1290,7 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
     value_caps = SDL_stack_alloc(HIDP_VALUE_CAPS, caps.NumberInputValueCaps);
     if (SDL_HidP_GetValueCaps(HidP_Input, value_caps, &caps.NumberInputValueCaps, ctx->preparsed_data) != HIDP_STATUS_SUCCESS) {
         RAWINPUT_JoystickClose(joystick);
+        SDL_stack_free(button_caps);
         return SDL_SetError("Couldn't get device value capabilities");
     }
 
@@ -1318,6 +1319,8 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         ctx->button_indices = (USHORT *)SDL_malloc(joystick->nbuttons * sizeof(*ctx->button_indices));
         if (!ctx->button_indices) {
             RAWINPUT_JoystickClose(joystick);
+            SDL_stack_free(value_caps);
+            SDL_stack_free(button_caps);
             return SDL_OutOfMemory();
         }
 
@@ -1365,12 +1368,15 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         joystick->naxes += 1;
     }
 
+    SDL_stack_free(button_caps);
+
     if (joystick->naxes > 0) {
         int axis_index = 0;
 
         ctx->axis_indices = (USHORT *)SDL_malloc(joystick->naxes * sizeof(*ctx->axis_indices));
         if (!ctx->axis_indices) {
             RAWINPUT_JoystickClose(joystick);
+            SDL_stack_free(value_caps);
             return SDL_OutOfMemory();
         }
 
@@ -1404,6 +1410,7 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         ctx->hat_indices = (USHORT *)SDL_malloc(joystick->nhats * sizeof(*ctx->hat_indices));
         if (!ctx->hat_indices) {
             RAWINPUT_JoystickClose(joystick);
+            SDL_stack_free(value_caps);
             return SDL_OutOfMemory();
         }
 
@@ -1422,6 +1429,8 @@ static int RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
         }
     }
 
+    SDL_stack_free(value_caps);
+
     joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
 
     return 0;

@icculus
Copy link
Collaborator

icculus commented Aug 24, 2024

This patch looks correct from here.

@sezero sezero closed this as completed in 8452123 Aug 25, 2024
sezero added a commit that referenced this issue Aug 25, 2024
sezero added a commit that referenced this issue Aug 25, 2024
Fixes #10574.
(cherry picked from commit 8452123)
(cherry picked from commit 4eac44b)
@sezero
Copy link
Contributor

sezero commented Aug 25, 2024

OK, patch applied to all active branches and pushed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants