Implementing a Custom Gesture Recognizer in Objective-C Causes Crashes on iOS 16.2
I've looked through the documentation and I'm still confused about Quick question that's been bugging me - Hey everyone, I'm running into an issue that's driving me crazy... I'm trying to debug I'm experimenting with I'm optimizing some code but I'm experimenting with I'm upgrading from an older version and I've been banging my head against this for hours. I'm relatively new to this, so bear with me. I'm trying to implement a custom gesture recognizer in my iOS app using Objective-C, but I'm facing crashes that seem to be related to my gesture handling logic. My gesture recognizer is supposed to recognize a double-tap on a specific UIView, but when I double-tap quickly, the app crashes with the following error: ``` Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<MyView: 0x7fb8d3c0bf30>) has been deallocated while still in use!' ``` I've defined my custom gesture recognizer like this: ```objc @interface MyCustomTapGestureRecognizer : UITapGestureRecognizer @end @implementation MyCustomTapGestureRecognizer - (instancetype)init { self = [super init]; if (self) { [self addTarget:self action:@selector(handleTap:)]; } return self; } - (void)handleTap:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateRecognized) { NSLog(@"Double tap recognized!"); } } @end ``` In my view controller, I add the gesture recognizer like this: ```objc self.customTapRecognizer = [[MyCustomTapGestureRecognizer alloc] init]; [self.myView addGestureRecognizer:self.customTapRecognizer]; ``` However, I suspect that the crash is related to how I'm managing the gesture recognizer's lifecycle. The `customTapRecognizer` is a property of my view controller, but I haven't always retained it correctly in some cases, especially when navigating away from the view controller. I've tried using `weak` references, but that might be causing the gesture recognizer to deallocate prematurely. I also noticed that implementing a single tap gesture works flawlessly, but the double tap seems to trigger this crash intermittently. I've checked that the view is still in the hierarchy when the gesture is recognized. Does anyone have insights on how to properly manage gesture recognizers in Objective-C, especially in relation to their lifecycle and memory management? For context: I'm using Objective-C on macOS. This is part of a larger service I'm building. What am I doing wrong? The stack includes Objective-C and several other technologies. Is this even possible? I'm on CentOS using the latest version of Objective-C. Am I approaching this the right way? Could this be a known issue? For reference, this is a production desktop app. Could this be a known issue? My team is using Objective-C for this mobile app. I've been using Objective-C for about a year now.