Leaked Android timers

In my application I am trying to get particles coming from maybe multiple touch points.

I have a class to store a point and an associated timer:

private static class Touch
{
    public Timer timer = new Timer();
    public vec position = new vec();
}

      

Inside onTouchEvent, in the case of MotionEvent.ACTION_POINTER_DOWN, I add a new Touch:

Touch t = new Touch();
t.position = new vec(event.getX(id), event.getY(id));
t.timer.scheduleAtFixedRate(new TimeredTouchTask(t.position, id), 0, SHOOTING_INTERVAL);

      

and store it inside the card using the touch id as the key.

If MotionEvent.ACTION_POINTER_UP fires, I remove the touch by calling cancel () on the timer and removing it from the map.

Finally, if the event is of type MotionEvent.ACTION_MOVE, I simply iterate over all the Touch instances inside the map and update the vector coordinates.

It works, but sometimes the timers don't get deleted correctly, sometimes they stay alive doing nothing, and sometimes they keep shooting particles even if I stop touching the screen.

How can I make this code more robust so that it always behaves correctly?

edit1: code ACTION_POINTER_UP

I am calling this method

private void removeTouch(int id)
{
    Touch t = _touch.get(id);

    if(t != null)
    {
        if(t.timer != null)
        {
            t.timer.cancel();
            t.timer = null;
        }

        t = null;
        _touch.remove(id);
    }
}

      

edit2: complete list REFERENCE

After reading some magazines, I learned that, for example: Touch finger # 1 Touch finger # 2 Touch finger # 3 Release finger # 3 Release finger # 2 Release finger # 1

ok but does:

Sensor finger # 1 Sensor finger # 2 Sensor finger # 3 Release finger # 1 Release finger # 2 Release finger # 3

leak of the last timer. I think I was finally messing around with IDs and indexes.

+3


source to share


1 answer


Judging from the usage getX(id)

, etc., I am assuming that id

is a pointer to a pointer, not a pointer id. Pointer indices are not guaranteed to persist for touch events, so you cannot find the same timer when you use it for subsequent events.

You have to use the tracking pointer id that is. getPointerId () will give you this for each index.



If you haven't read it yet, I highly recommend reading "Make it a Multitouch Feel" on the Android blog.

+1


source







All Articles