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

Result of SDL_GetGamepads must be de-allocated by SDL_free #7918

Conversation

MattGuerrette
Copy link
Contributor

This PR fixes a memory leak in the SDL3 backend where the gamepad list returned by SDL_GetGamepads was not being deallocated.

Noticed by running under the Xcode Instruments leak detector.

@ocornut
Copy link
Owner

ocornut commented Aug 25, 2024

See #7898
i believe you may need to update your copy of SDL

@MattGuerrette
Copy link
Contributor Author

MattGuerrette commented Aug 25, 2024

#7898

I am using mainline of the SDL repo, as my CMake fetch content script pulls down the latest.

You can see here: https://github.com/MattGuerrette/Metal/blob/db4360991f93a0c8ad68edd44f2d6853355f4274/cmake/FetchExternal.cmake#L23

Also, the mainline documentation of SDL3 now states:

/**
 * Get a list of currently connected gamepads.
 *
 * \param count a pointer filled in with the number of gamepads returned, may
 *              be NULL.
 * \returns a 0 terminated array of joystick instance IDs or NULL on failure;
 *          call SDL_GetError() for more information. This should be freed
 *          with SDL_free() when it is no longer needed.
 *
 * \since This function is available since SDL 3.0.0.
 *
 * \sa SDL_HasGamepad
 * \sa SDL_OpenGamepad
 */

@mgerhardy
Copy link

Unfortunately not: https://github.com/libsdl-org/SDL/blob/4961af4569eaaec8d2c9cbbd213236e6b7e2f1dd/include/SDL3/SDL_joystick.h#L216 - see the linked ticket there are more details.

@MattGuerrette
Copy link
Contributor Author

Unfortunately not: https://github.com/libsdl-org/SDL/blob/4961af4569eaaec8d2c9cbbd213236e6b7e2f1dd/include/SDL3/SDL_joystick.h#L216 - see the linked ticket there are more details.

This is certainly a leak, as when should this be cleaned up then?

https://github.com/libsdl-org/SDL/blob/d9a5ed75b969af481a3da882ca87def205499513/src/joystick/SDL_joystick.c#L730

They are clearly allocating this joystick array and never freeing it. I don't think Xcode Instruments is confused about this at all.

I could file a ticket to SDL repo to fix that documentation, but for now I'd suggest doing what the SDL_Gamepad documentation clearly states and de-allocating this array:
https://github.com/libsdl-org/SDL/blob/d9a5ed75b969af481a3da882ca87def205499513/include/SDL3/SDL_gamepad.h#L473

@ocornut
Copy link
Owner

ocornut commented Aug 27, 2024

Looks like they made a large change to the memory system and then reverted it end of july, that explains things:
libsdl-org/SDL@4f55271

ocornut pushed a commit that referenced this pull request Sep 3, 2024
@ocornut
Copy link
Owner

ocornut commented Sep 3, 2024

Merged as 6a73195. Thank you!

@ocornut ocornut closed this Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants