From f81c6c52fcdd01069ed7f4bcd5fc941fc70d7370 Mon Sep 17 00:00:00 2001 From: Sergey Minakov Date: Tue, 25 Aug 2020 15:18:47 +0300 Subject: [PATCH] iOS: fix deprecations and leaks Fixes some deprecations and leaks reported by Xcode Analyzer --- platform/iphone/app_delegate.mm | 13 +++++++------ platform/iphone/gl_view.h | 2 +- platform/iphone/gl_view.mm | 8 +++++--- platform/iphone/gl_view_gesture_recognizer.m | 13 +++++++++---- platform/iphone/view_controller.h | 3 --- platform/iphone/view_controller.mm | 17 ++++++++--------- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm index 4de321fa04a..2f3a497e7af 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -49,8 +49,10 @@ void _set_keep_screen_on(bool p_enabled); Error _shell_open(String p_uri) { NSString *url = [[NSString alloc] initWithUTF8String:p_uri.utf8().get_data()]; - if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) + if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) { + [url release]; return ERR_CANT_OPEN; + } printf("opening url %ls\n", p_uri.c_str()); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; @@ -548,7 +550,7 @@ static int frame_count = 0; // can use that instead? (note that left and right seem swapped) switch ([[UIApplication sharedApplication] statusBarOrientation]) { - case UIDeviceOrientationLandscapeLeft: { + case UIInterfaceOrientationLandscapeLeft: { OSIPhone::get_singleton()->update_gravity(-gravity.y, gravity.x, gravity.z); OSIPhone::get_singleton()->update_accelerometer( @@ -559,7 +561,7 @@ static int frame_count = 0; OSIPhone::get_singleton()->update_gyroscope(-rotation.y, rotation.x, rotation.z); }; break; - case UIDeviceOrientationLandscapeRight: { + case UIInterfaceOrientationLandscapeRight: { OSIPhone::get_singleton()->update_gravity(gravity.y, -gravity.x, gravity.z); OSIPhone::get_singleton()->update_accelerometer( @@ -570,7 +572,7 @@ static int frame_count = 0; OSIPhone::get_singleton()->update_gyroscope(rotation.y, -rotation.x, rotation.z); }; break; - case UIDeviceOrientationPortraitUpsideDown: { + case UIInterfaceOrientationPortraitUpsideDown: { OSIPhone::get_singleton()->update_gravity(-gravity.x, gravity.y, gravity.z); OSIPhone::get_singleton()->update_accelerometer( @@ -595,7 +597,7 @@ static int frame_count = 0; }; } - bool quit_request = OSIPhone::get_singleton()->iterate(); + OSIPhone::get_singleton()->iterate(); }; }; break; @@ -614,7 +616,6 @@ static int frame_count = 0; is_focus_out = false; - [application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone]; // disable idle timer // application.idleTimerDisabled = YES; diff --git a/platform/iphone/gl_view.h b/platform/iphone/gl_view.h index a51a43417b3..5de13da443e 100644 --- a/platform/iphone/gl_view.h +++ b/platform/iphone/gl_view.h @@ -107,7 +107,7 @@ - (void)keyboardOnScreen:(NSNotification *)notification; - (void)keyboardHidden:(NSNotification *)notification; -@property NSTimeInterval animationInterval; +@property(nonatomic, assign) NSTimeInterval animationInterval; @property(nonatomic, assign) BOOL useCADisplayLink; @end diff --git a/platform/iphone/gl_view.mm b/platform/iphone/gl_view.mm index d03a1e64403..a4a00a09782 100644 --- a/platform/iphone/gl_view.mm +++ b/platform/iphone/gl_view.mm @@ -80,10 +80,12 @@ void _hide_keyboard() { }; Rect2 _get_ios_window_safe_area(float p_window_width, float p_window_height) { - UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0); - if (_instance != nil && [_instance respondsToSelector:@selector(safeAreaInsets)]) { + UIEdgeInsets insets = UIEdgeInsetsZero; + + if (@available(iOS 11.0, *)) { insets = [_instance safeAreaInsets]; } + ERR_FAIL_COND_V(insets.left < 0 || insets.top < 0 || insets.right < 0 || insets.bottom < 0, Rect2(0, 0, p_window_width, p_window_height)); UIEdgeInsets window_insets = UIEdgeInsetsMake(_points_to_pixels(insets.top), _points_to_pixels(insets.left), _points_to_pixels(insets.bottom), _points_to_pixels(insets.right)); @@ -699,7 +701,7 @@ static void clear_touches() { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == _instance.avPlayerItem && [keyPath isEqualToString:@"status"]) { - if (_instance.avPlayerItem.status == AVPlayerStatusFailed || _instance.avPlayer.status == AVPlayerStatusFailed) { + if (_instance.avPlayerItem.status == AVPlayerItemStatusFailed || _instance.avPlayer.status == AVPlayerStatusFailed) { _stop_video(); video_found_error = true; } diff --git a/platform/iphone/gl_view_gesture_recognizer.m b/platform/iphone/gl_view_gesture_recognizer.m index ff2610a235d..7a568a262be 100644 --- a/platform/iphone/gl_view_gesture_recognizer.m +++ b/platform/iphone/gl_view_gesture_recognizer.m @@ -68,17 +68,18 @@ const CGFloat kGLGestureMovementDistance = 0.5; [self.view touchesBegan:delayedTouches withEvent:delayedEvent]; } + [delayedTouches release]; delayedTouches = nil; delayedEvent = nil; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - NSSet *cleared = [self clearTouches:touches phase:UITouchPhaseBegan]; + NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseBegan]; [self delayTouches:cleared andEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - NSSet *cleared = [self clearTouches:touches phase:UITouchPhaseMoved]; + NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseMoved]; if (delayTimer) { // We should check if movement was significant enough to fire an event @@ -95,20 +96,24 @@ const CGFloat kGLGestureMovementDistance = 0.5; if (distance > kGLGestureMovementDistance) { [delayTimer fire]; [self.view touchesMoved:cleared withEvent:event]; + [cleared release]; return; } } + [cleared release]; return; } [self.view touchesMoved:cleared withEvent:event]; + [cleared release]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [delayTimer fire]; - NSSet *cleared = [self clearTouches:touches phase:UITouchPhaseEnded]; + NSSet *cleared = [self copyClearedTouches:touches phase:UITouchPhaseEnded]; [self.view touchesEnded:cleared withEvent:event]; + [cleared release]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { @@ -116,7 +121,7 @@ const CGFloat kGLGestureMovementDistance = 0.5; [self.view touchesCancelled:touches withEvent:event]; }; -- (NSSet *)clearTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave { +- (NSSet *)copyClearedTouches:(NSSet *)touches phase:(UITouchPhase)phaseToSave { NSMutableSet *cleared = [touches mutableCopy]; for (UITouch *touch in touches) { diff --git a/platform/iphone/view_controller.h b/platform/iphone/view_controller.h index f6bbe11d97d..bb5fec83682 100644 --- a/platform/iphone/view_controller.h +++ b/platform/iphone/view_controller.h @@ -34,9 +34,6 @@ @interface ViewController : UIViewController { }; -- (BOOL)shouldAutorotateToInterfaceOrientation: - (UIInterfaceOrientation)p_orientation; - - (void)didReceiveMemoryWarning; - (void)viewDidLoad; diff --git a/platform/iphone/view_controller.mm b/platform/iphone/view_controller.mm index 465e38e45e6..7653f9f0710 100644 --- a/platform/iphone/view_controller.mm +++ b/platform/iphone/view_controller.mm @@ -42,12 +42,12 @@ int add_cmdline(int, char **); int add_path(int p_argc, char **p_args) { NSString *str = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"godot_path"]; - if (!str) + if (!str) { return p_argc; + } - p_args[p_argc++] = "--path"; - [str retain]; // memory leak lol (maybe make it static here and delete it in ViewController destructor? @todo - p_args[p_argc++] = (char *)[str cString]; + p_args[p_argc++] = (char *)"--path"; + p_args[p_argc++] = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding]; p_args[p_argc] = NULL; return p_argc; @@ -60,12 +60,11 @@ int add_cmdline(int p_argc, char **p_args) { return p_argc; for (int i = 0; i < [arr count]; i++) { - NSString *str = [arr objectAtIndex:i]; - if (!str) + if (!str) { continue; - [str retain]; // @todo delete these at some point - p_args[p_argc++] = (char *)[str cString]; + } + p_args[p_argc++] = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding]; }; p_args[p_argc] = NULL; @@ -81,7 +80,7 @@ int add_cmdline(int p_argc, char **p_args) { @implementation ViewController - (void)didReceiveMemoryWarning { - + [super didReceiveMemoryWarning]; printf("*********** did receive memory warning!\n"); };