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

fix memory leak and maintain joysticks list #8

Merged
merged 1 commit into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/QJoysticks/SDL_Joysticks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,7 @@ SDL_Joysticks::~SDL_Joysticks()
*/
QList<QJoystickDevice*> SDL_Joysticks::joysticks()
{
QList<QJoystickDevice*> list;

#ifdef SDL_SUPPORTED
for (int i = 0; i < SDL_NumJoysticks(); ++i)
list.append (getJoystick (i));
#endif

return list;
return m_joysticks;
}

/**
Expand Down Expand Up @@ -133,6 +126,8 @@ void SDL_Joysticks::update()
case SDL_JOYDEVICEREMOVED:
SDL_JoystickClose (SDL_JoystickOpen (event.jdevice.which));
SDL_GameControllerClose (SDL_GameControllerOpen (event.cdevice.which));
delete m_joysticks[event.cdevice.which];
m_joysticks.removeAt(event.cdevice.which);
emit countChanged();
break;
case SDL_JOYAXISMOTION:
Expand Down Expand Up @@ -187,6 +182,8 @@ void SDL_Joysticks::configureJoystick (const SDL_Event* event)

SDL_GameControllerOpen (event->cdevice.which);

m_joysticks.append(getJoystick(event->cdevice.which));

++m_tracker;
emit countChanged();
#else
Expand Down Expand Up @@ -270,7 +267,6 @@ QJoystickPOVEvent SDL_Joysticks::getPOVEvent (const SDL_Event* sdl_event)

#ifdef SDL_SUPPORTED
event.pov = sdl_event->jhat.hat;
event.joystick = getJoystick (sdl_event->jdevice.which);

switch (sdl_event->jhat.value) {
case SDL_HAT_RIGHTUP:
Expand Down Expand Up @@ -301,6 +297,8 @@ QJoystickPOVEvent SDL_Joysticks::getPOVEvent (const SDL_Event* sdl_event)
event.angle = -1;
break;
}
event.joystick = m_joysticks[sdl_event->cdevice.which];
event.joystick->axes[event.pov] = event.angle;
#else
Q_UNUSED (sdl_event);
#endif
Expand All @@ -319,7 +317,8 @@ QJoystickAxisEvent SDL_Joysticks::getAxisEvent (const SDL_Event* sdl_event)
#ifdef SDL_SUPPORTED
event.axis = sdl_event->caxis.axis;
event.value = static_cast<qreal> (sdl_event->caxis.value) / 32767;
event.joystick = getJoystick (sdl_event->cdevice.which);
event.joystick = m_joysticks[sdl_event->cdevice.which];
event.joystick->axes[event.axis] = event.value;
#else
Q_UNUSED (sdl_event);
#endif
Expand All @@ -339,7 +338,8 @@ QJoystickButtonEvent SDL_Joysticks::getButtonEvent (const SDL_Event*
#ifdef SDL_SUPPORTED
event.button = sdl_event->jbutton.button;
event.pressed = sdl_event->jbutton.state == SDL_PRESSED;
event.joystick = getJoystick (sdl_event->jdevice.which);
event.joystick = m_joysticks[sdl_event->cdevice.which];
event.joystick->axes[event.button] = event.pressed;
#else
Q_UNUSED (sdl_event);
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/QJoysticks/SDL_Joysticks.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private slots:
QJoystickButtonEvent getButtonEvent (const SDL_Event* sdl_event);

int m_tracker;
QList<QJoystickDevice> m_joysticks;
QList<QJoystickDevice*> m_joysticks;
};

#endif