From 9c97630a22ec99bafb687e8aff25f21a25acccb0 Mon Sep 17 00:00:00 2001 From: Changkun Ou Date: Fri, 27 Aug 2021 10:53:05 +0200 Subject: [PATCH] app: protect out of touchIDs From practical observation, out of touchIDs may occur. This conflicts the maximum number of handled touchs in iOS devices. Therefore the only reason that may cause out of touchIDs is that the touchIDs are intrinsically non-concurrent safe. This change adds a read-write lock to the touchIDs array. Fixes fyne-io/fyne#2407. --- app/darwin_ios.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/darwin_ios.go b/app/darwin_ios.go index 67afe4362..23ac4324a 100644 --- a/app/darwin_ios.go +++ b/app/darwin_ios.go @@ -125,7 +125,12 @@ func updateConfig(width, height, orientation int32) { // // It is widely reported that the iPhone can handle up to 5 simultaneous // touch events, while the iPad can handle 11. -var touchIDs [11]uintptr +var ( + // touchIDs may arrive concurrently, use a lock for safety. + // See https://github.com/fyne-io/fyne/issues/2407. + touchMu sync.RWMutex + touchIDs [11]uintptr +) var touchEvents struct { sync.Mutex @@ -135,12 +140,17 @@ var touchEvents struct { //export sendTouch func sendTouch(cTouch, cTouchType uintptr, x, y float32) { id := -1 + + touchMu.RLock() for i, val := range touchIDs { if val == cTouch { id = i break } } + touchMu.RUnlock() + + touchMu.Lock() if id == -1 { for i, val := range touchIDs { if val == 0 { @@ -158,6 +168,7 @@ func sendTouch(cTouch, cTouchType uintptr, x, y float32) { if t == touch.TypeEnd { touchIDs[id] = 0 } + touchMu.Unlock() theApp.eventsIn <- touch.Event{ X: x,