Fixed bug 2418 - Structure SDL_gestureTouch leaking
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 13:48:13 -0700
changeset 11290ef96b05151c5
parent 11289 ca3c2c98f2e1
child 11291 a153a5c6d413
Fixed bug 2418 - Structure SDL_gestureTouch leaking

Leonardo

Structure SDL_gestureTouch gets reallocated for every new added gesture but its never freed.

Proposed patch add the function SDL_GestureQuit() that takes care of doing that and gets called when TouchQuit is called.

Gabriel Jacobo

Thanks for the patch. I think it needs a bit of extra work though, looking at the code in SDL_gesture.c , I see that SDL_numGestureTouches only goes up, I think the right fix here involves adding SDL_GestureDelTouch (hooked into SDL_DelTouch) as well as SDL_GestureQuit (as you posted in your patch).
src/events/SDL_gesture.c
src/events/SDL_gesture_c.h
src/events/SDL_touch.c
     1.1 --- a/src/events/SDL_gesture.c	Mon Aug 14 13:37:14 2017 -0700
     1.2 +++ b/src/events/SDL_gesture.c	Mon Aug 14 13:48:13 2017 -0700
     1.3 @@ -101,6 +101,12 @@
     1.4      return (touchId < 0);
     1.5  }
     1.6  
     1.7 +void SDL_GestureQuit()
     1.8 +{
     1.9 +    SDL_free(SDL_gestureTouch);
    1.10 +    SDL_gestureTouch = NULL;
    1.11 +}
    1.12 +
    1.13  static unsigned long SDL_HashDollar(SDL_FloatPoint* points)
    1.14  {
    1.15      unsigned long hash = 5381;
    1.16 @@ -457,6 +463,28 @@
    1.17      return 0;
    1.18  }
    1.19  
    1.20 +int SDL_GestureDelTouch(SDL_TouchID touchId)
    1.21 +{
    1.22 +    int i;
    1.23 +    for (i = 0; i < SDL_numGestureTouches; i++) {
    1.24 +        if (SDL_gestureTouch[i].id == touchId) {
    1.25 +            break;
    1.26 +        }
    1.27 +    }
    1.28 +
    1.29 +    if (i == SDL_numGestureTouches) {
    1.30 +        /* not found */
    1.31 +        return -1;
    1.32 +    }
    1.33 +
    1.34 +    SDL_free(SDL_gestureTouch[i].dollarTemplate);
    1.35 +    SDL_zero(SDL_gestureTouch[i]);
    1.36 +
    1.37 +    SDL_numGestureTouches--;
    1.38 +    SDL_gestureTouch[i] = SDL_gestureTouch[SDL_numGestureTouches];
    1.39 +    return 0;
    1.40 +}
    1.41 +
    1.42  static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
    1.43  {
    1.44      int i;
     2.1 --- a/src/events/SDL_gesture_c.h	Mon Aug 14 13:37:14 2017 -0700
     2.2 +++ b/src/events/SDL_gesture_c.h	Mon Aug 14 13:48:13 2017 -0700
     2.3 @@ -24,9 +24,12 @@
     2.4  #define SDL_gesture_c_h_
     2.5  
     2.6  extern int SDL_GestureAddTouch(SDL_TouchID touchId);
     2.7 +extern int SDL_GestureDelTouch(SDL_TouchID touchId);
     2.8  
     2.9  extern void SDL_GestureProcessEvent(SDL_Event* event);
    2.10  
    2.11 +extern void SDL_GestureQuit(void);
    2.12 +
    2.13  #endif /* SDL_gesture_c_h_ */
    2.14  
    2.15  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/events/SDL_touch.c	Mon Aug 14 13:37:14 2017 -0700
     3.2 +++ b/src/events/SDL_touch.c	Mon Aug 14 13:48:13 2017 -0700
     3.3 @@ -352,6 +352,9 @@
     3.4  
     3.5      SDL_num_touch--;
     3.6      SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
     3.7 +
     3.8 +    /* Delete this touch device for gestures */
     3.9 +    SDL_GestureDelTouch(id);
    3.10  }
    3.11  
    3.12  void
    3.13 @@ -366,6 +369,7 @@
    3.14  
    3.15      SDL_free(SDL_touchDevices);
    3.16      SDL_touchDevices = NULL;
    3.17 +    SDL_GestureQuit();
    3.18  }
    3.19  
    3.20  /* vi: set ts=4 sw=4 expandtab: */