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

Joystick Handle leak #4259

Closed
twisted89 opened this issue Mar 31, 2021 · 3 comments
Closed

Joystick Handle leak #4259

twisted89 opened this issue Mar 31, 2021 · 3 comments
Assignees
Labels
waiting Waiting on user response

Comments

@twisted89
Copy link

twisted89 commented Mar 31, 2021

With a Dualshock 4 connected and HID support disabled SDL2 leaks User Objects like there's no tomorrow.
Tested on Windows 10 pro x64 build 19042.870

image

I've noticed the issue doesn't occur when SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1"); is set

Issue easily reproducible with latest official DLL '2.0.14' and the following test code (with a Dualshock 4 connected via USB)

#include <iostream>
#include <vector>
#include <SDL.h>
#include <Windows.h>

static int sdlinput_js_init() {
    
    // SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1");
    SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4, "0");
    
    if (SDL_Init(0) < 0) {
        printf("SDL_Init failed");
        return -1;
    }
    
    if (SDL_InitSubSystem((SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC)) < 0) {
        printf("SDL_InitSubSystem failed");
        return -1;
    }
    
    return 0;
}
    
static void sdl_peep_events() {
    
    static SDL_Event sdl_events[256];
    unsigned int minType = SDL_JOYAXISMOTION;
    unsigned int maxType = SDL_CONTROLLERDEVICEREMAPPED; 
    SDL_PeepEvents(sdl_events, 256, SDL_GETEVENT, minType, maxType);
}

#undef main
int main()
{
    sdlinput_js_init();
    while (true)
    {
        sdl_peep_events();
        Sleep(10);
    }
    
    return 0;
}
@icculus icculus added this to the 2.0.16 milestone Apr 1, 2021
@slouken
Copy link
Collaborator

slouken commented Jul 24, 2021

I wasn't able to reproduce this with the latest code snapshot, can you try that and see if this is fixed?
http://www.libsdl.org/tmp/SDL-2.0.zip

@slouken slouken added the waiting Waiting on user response label Jul 24, 2021
@slouken slouken removed this from the 2.0.16 milestone Aug 4, 2021
@clavallee-dbox
Copy link

clavallee-dbox commented Oct 22, 2021

Hello,
I have the same issue.
Tested on Windows 10 Pro x64 build 19043 with Xbox 360 Controller (and other controllers too).
I tested with SDL version 2.0.16, 2.0.14 and also the zip version you posted

SDL_LEAK

It only goes up when I press buttons/move axis around on my controller. If no controller inputs, User objects does not increase.
If I do SDL_PollEvent the problem goes away, or as mentioned above SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1"); also makes the problem go away.

#include "SDL.h"
#include <iostream>
#include <chrono>
#include <thread>

namespace {
	void PollEvents() {
		SDL_Event oEvent;
		while (SDL_PollEvent(&oEvent)) {
		}
	}
}

#undef main
int main(int argc, char* argv[]) {

	// SDL_SetHint(SDL_HINT_JOYSTICK_THREAD, "1"); // uncomment this and no more leaks

	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		return false;
	}
	
	while (true) {
		// PollEvents(); // OR uncomment this and no more leaks
		std::this_thread::sleep_for(std::chrono::milliseconds(100));
	}

	SDL_Quit();
}

@slouken
Copy link
Collaborator

slouken commented Nov 7, 2023

You should definitely poll for events or use the event thread so Windows can clean up event handles.

@slouken slouken closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting Waiting on user response
Projects
None yet
Development

No branches or pull requests

4 participants