Imported from the Apple Support Forums via the Wayback Machine 🔗
I'm trying to keep track of a multitouch 'phase' by storing the addresses of the UITouch events as they come in. However, it seems to me like the OS is reusing addresses. Please note the chain of events below. The touch at address 0013CA40 begins, moves, and ends; then a touch at address 0013CA40 begins again. The 'ended' and last 'began' occurred at such a close proximity, that my program tried to process both during the next iteration of my runloop and asserted. How am I supposed to keep track of the next 'phase' that just began, if I'm still processing the 'ended' of a different phase in the same call of my runloop?
22:22:09.823 touchesBegan:withEvent: Recording 0013CA40 of phase=UITouchPhaseBegan
22:22:09.851 touchesMoved:withEvent: Recording 0013CA40 of phase=UITouchPhaseMoved
22:22:09.875 touchesEnded:withEvent: Recording 0013CA40 of phase=UITouchPhaseEnded
22:22:09.890 touchesBegan:withEvent: Recording 0013CA40 of phase=UITouchPhaseBegan
Imported from the Apple Support Forums via the Wayback Machine 🔗
iradel wrote:
it seems to me like the OS is reusing addresses.
I think you're observing the documented behavior. From the [UITouch|http://developer.apple.com/iphone/library/documentation/UIKit/Referenc e/UITouch Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006785-CH3-DontLinkElemen tID1] and [UIEvent|http://developer.apple.com/iphone/library/documentation/UIKit/Referenc e/UIEvent Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006780-CH3-DontLinkElemen tID1] Overviews, respectively:
A UITouch object is persistent throughout a multi-touch sequence. You should never retain an UITouch object when handling an event. If you need to keep information about a touch from one phase to another, you should copy that information from the UITouch object.
A UIEvent object representing a touch event is persistent throughout a multi-touch sequence; UIKit reuses the same UIEvent instance for every event delivered to the application. You should never retain an event object or any object returned from an event object. If you need to keep information from an event around from one phase to another, you should copy that information from the UITouch or UIEvent object.
How am I supposed to keep track of the next 'phase' that just began, if I'm still processing the 'ended' of a different phase in the same call of my runloop?
I don't think I understand what you're describing. Event objects don't arrive from different threads, and all the touches for one event should be packaged in one event object. Could you please take another look at [Events and Touches|http://developer.apple.com/iphone/library/documentation/iPhone/Conceptu al/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple_ref/doc/uid /TP40007072-CH9-SW8] through [Handling a Complex Multi-Touch Sequence|http://developer.apple.com/iphone/library/documentation/iPhone/Concept ual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple_ref/doc/ui d/TP40007072-CH9-SW16] in the +iPhone Application Programming Guide+, then let us know what's happening in your project that doesn't fit that model? I might also drop some of the example code from those sections into a testbed to see whether it operates differently from your code.
If none of the above is helpful, you're more than welcome to post some of your code so one of our Cocoa Touch experts can take a look at it.
- Ray
0
iradel·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I understand that UITouch objects are internally persistent throughout the life of a multi-touch event (first finger down to last finger up). I understand that you can 'key' on the address of a UITouch object to store data about it as the touch progresses (e.g. store beginning location and find the same touch later, by comparing its address to the one you stored, to get its moved or ending location).
My question is, what protection does the OS/system offer against UITouch objects in different and independent multi-touches reusing the same address? E.g.:
[ internal ] UITouch is created at address 0xA
[ app ] callback touchesBegan outside of runloop (store data about touch 'keyed' by 0xA)
[ app ] callback touchesMoved outside of runloop for UITouch* at 0xA. We find 0xA in our cache and update variables
[ app ] callback touhesEnded outside of runloop for UITouch* at 0xA.
[ internal ] UITouch object at address 0xA is deleted. Previous multi-touch is done.
[ internal ] new UITouch object is created for a new multi-touch. Address should presumably not be 0xA, but it appears that the object is randomly (?) assigned 0xA.
[ app ] callback touhesBegan outside of runloop for UITouch* at 0xA.
[ app ] Runloop is invoked. App is confused because it has to process a stored 'touch ended' for an UITouch object at address 0xA and a 'touch began' for a UITouch object at address 0xA.
Thank you very much.
0
iradel·
Imported from the Apple Support Forums via the Wayback Machine 🔗
It would seemingly make a lot more sense to use GUIDs rather than having end-users store UITouch* (but warning them not to retain, etc.). But that's another question entirely.
0
sptrakesh·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I think the only guaranteed behaviour is that the same UITouch will be used throughout a single event sequence. As the documentation mentions UIKit is maintaining a single or a pool of UITouch instances that it will reuse for different sequence of events (object pooling is not an uncommon practise). From the programmers perspective once you have handled a touches ended callback, you must "release" all/any references to have to the touch instance.
0
VietTran·
Imported from the Apple Support Forums via the Wayback Machine 🔗
@iradel
You are right; the touch addresses is reused from UITouch pool. For your case, however, it is the same touch since tapCount has to be calculated. If you print out tapCount, you will see it increases.
Between touchesBegan and touchesEnd phase, the touch address is unique. You won't have any problem unless you tried to keep the touch after its Ended phase; then you are in big trouble in this very convoluted touch management.
I'm doing Palm Detection for my handwriting app Notes+ and I have to keep the touches after it ended. I have played around a lot with these UITouch. Contact me if you want deep analysis in this http://viet-tran.com