From d586287af741cc5070d69342693f58afa7ea6b28 Mon Sep 17 00:00:00 2001 From: parveshneedhoo Date: Wed, 10 Jul 2024 16:43:01 +0400 Subject: [PATCH 1/3] Deallocate unused resource in an orderly manner --- src/ios/CameraRenderController.h | 2 + src/ios/CameraRenderController.m | 68 +++++++++++++++++++------------- src/ios/CameraSessionManager.m | 1 + src/ios/SimpleCameraPreview.m | 7 ++-- 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/ios/CameraRenderController.h b/src/ios/CameraRenderController.h index 61c7c3f..669ac59 100644 --- a/src/ios/CameraRenderController.h +++ b/src/ios/CameraRenderController.h @@ -19,6 +19,8 @@ CVOpenGLESTextureRef _lumaTexture; } +-(void) deallocateRenderMemory; + @property (nonatomic) CameraSessionManager *sessionManager; @property (nonatomic) CIContext *ciContext; @property (nonatomic) CIImage *latestFrame; diff --git a/src/ios/CameraRenderController.m b/src/ios/CameraRenderController.m index 9c7b331..9f288a4 100644 --- a/src/ios/CameraRenderController.m +++ b/src/ios/CameraRenderController.m @@ -56,16 +56,17 @@ - (void) viewWillAppear:(BOOL)animated { }); } -- (void) viewWillDisappear:(BOOL)animated { - [super viewWillDisappear:animated]; - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; - [self.view removeFromSuperview]; - [EAGLContext setCurrentContext:nil]; - self.context = nil; - [self deallocateRenderMemory]; - self.ciContext = nil; -} +//- (void) viewWillDisappear:(BOOL)animated { +// NSLog(@"HELLO 12"); +// [super viewWillDisappear:animated]; +// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; +// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; +// [self.view removeFromSuperview]; +// [EAGLContext setCurrentContext:nil]; +// self.context = nil; +// [self deallocateRenderMemory]; +// self.ciContext = nil; +//} - (void) appplicationIsActive:(NSNotification *)notification { dispatch_async(self.sessionManager.sessionQueue, ^{ @@ -157,23 +158,25 @@ -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMS } } -- (void)viewDidDisappear:(BOOL)animated { - [super viewDidDisappear:animated]; - - [EAGLContext setCurrentContext:nil]; - self.context = nil; - [self deallocateRenderMemory]; - self.ciContext = nil; - CVBufferRelease(_pixelBuffer); - _pixelBuffer = nil; -} - -- (void)dealloc { - [EAGLContext setCurrentContext:nil]; - self.context = nil; - [self deallocateRenderMemory]; - self.ciContext = nil; -} +//- (void)viewDidDisappear:(BOOL)animated { +// [super viewDidDisappear:animated]; +// NSLog(@"HELLO 14"); +// [self.view removeFromSuperview]; +// [EAGLContext setCurrentContext:nil]; +// self.context = nil; +// [self deallocateRenderMemory]; +// self.ciContext = nil; +// CVBufferRelease(_pixelBuffer); +// _pixelBuffer = nil; +//} + +//- (void)dealloc { +// NSLog(@"HELLO 15"); +// [EAGLContext setCurrentContext:nil]; +// self.context = nil; +// [self deallocateRenderMemory]; +// self.ciContext = nil; +//} - (BOOL)shouldAutorotate { return YES; @@ -200,6 +203,17 @@ -(void) deallocateRenderMemory { CFRelease(_videoTextureCache); _videoTextureCache = nil; } + if(_lumaTexture) { + CVOpenGLESTextureCacheFlush(_lumaTexture, 0); + CFRelease(_lumaTexture); + _lumaTexture = nil; + } + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; + [self.view removeFromSuperview]; + [EAGLContext setCurrentContext:nil]; + self.context = nil; + self.ciContext = nil; } @end diff --git a/src/ios/CameraSessionManager.m b/src/ios/CameraSessionManager.m index f71e7fa..3a685b9 100644 --- a/src/ios/CameraSessionManager.m +++ b/src/ios/CameraSessionManager.m @@ -284,6 +284,7 @@ - (void)deallocSession { if (self.sessionQueue) { self.sessionQueue = nil; } + self.device = nil; } @end diff --git a/src/ios/SimpleCameraPreview.m b/src/ios/SimpleCameraPreview.m index cf6de90..1d4e524 100644 --- a/src/ios/SimpleCameraPreview.m +++ b/src/ios/SimpleCameraPreview.m @@ -113,14 +113,14 @@ - (void) disable:(CDVInvokedUrlCommand*)command { for(AVCaptureOutput *output in self.sessionManager.session.outputs) { [self.sessionManager.session removeOutput:output]; } + self.sessionManager.delegate = nil; [self.sessionManager deallocSession]; self.sessionManager = nil; dispatch_async(dispatch_get_main_queue(), ^{ [self.cameraRenderController willMoveToParentViewController:nil]; [self.cameraRenderController.view removeFromSuperview]; - if(self.viewController.parentViewController != nil) { - [self.cameraRenderController removeFromParentViewController]; - } + [self.cameraRenderController removeFromParentViewController]; + [self.cameraRenderController deallocateRenderMemory]; self.cameraRenderController = nil; [self deallocateMemory]; [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; @@ -236,6 +236,7 @@ - (NSDictionary *)getGPSDictionaryForLocation { [gps setObject:[formatter stringFromDate:location.timestamp] forKey:(NSString *)kCGImagePropertyGPSTimeStamp]; [formatter setDateFormat:@"yyyy:MM:dd"]; [gps setObject:[formatter stringFromDate:location.timestamp] forKey:(NSString *)kCGImagePropertyGPSDateStamp]; + formatter = nil; // Latitude CGFloat latitude = location.coordinate.latitude; From eb0bd9a94af617a76149a83bcfa2a968fc7b8408 Mon Sep 17 00:00:00 2001 From: parveshneedhoo Date: Fri, 12 Jul 2024 10:55:09 +0400 Subject: [PATCH 2/3] remove commented code --- src/ios/CameraRenderController.m | 36 -------------------------------- 1 file changed, 36 deletions(-) diff --git a/src/ios/CameraRenderController.m b/src/ios/CameraRenderController.m index 9f288a4..9c992f1 100644 --- a/src/ios/CameraRenderController.m +++ b/src/ios/CameraRenderController.m @@ -56,18 +56,6 @@ - (void) viewWillAppear:(BOOL)animated { }); } -//- (void) viewWillDisappear:(BOOL)animated { -// NSLog(@"HELLO 12"); -// [super viewWillDisappear:animated]; -// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; -// [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; -// [self.view removeFromSuperview]; -// [EAGLContext setCurrentContext:nil]; -// self.context = nil; -// [self deallocateRenderMemory]; -// self.ciContext = nil; -//} - - (void) appplicationIsActive:(NSNotification *)notification { dispatch_async(self.sessionManager.sessionQueue, ^{ if (!self.sessionManager.session.running){ @@ -78,10 +66,6 @@ - (void) appplicationIsActive:(NSNotification *)notification { } - (void) applicationEnteredForeground:(NSNotification *)notification { -// dispatch_async(self.sessionManager.sessionQueue, ^{ -// NSLog(@"Stopping session"); -// [self.sessionManager.session stopRunning]; -// }); [self.view removeFromSuperview]; [EAGLContext setCurrentContext:nil]; self.context = nil; @@ -158,26 +142,6 @@ -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMS } } -//- (void)viewDidDisappear:(BOOL)animated { -// [super viewDidDisappear:animated]; -// NSLog(@"HELLO 14"); -// [self.view removeFromSuperview]; -// [EAGLContext setCurrentContext:nil]; -// self.context = nil; -// [self deallocateRenderMemory]; -// self.ciContext = nil; -// CVBufferRelease(_pixelBuffer); -// _pixelBuffer = nil; -//} - -//- (void)dealloc { -// NSLog(@"HELLO 15"); -// [EAGLContext setCurrentContext:nil]; -// self.context = nil; -// [self deallocateRenderMemory]; -// self.ciContext = nil; -//} - - (BOOL)shouldAutorotate { return YES; } From eb43ec4c23b3871483473f565a4e00fb4ff53813 Mon Sep 17 00:00:00 2001 From: parveshneedhoo Date: Fri, 12 Jul 2024 12:06:45 +0400 Subject: [PATCH 3/3] Bump plugin version from 2.0.21 to 2.0.22 --- CHANGELOG.md | 3 +++ package-lock.json | 4 ++-- package.json | 2 +- plugin.xml | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 715c13f..795cc15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [2.0.22](https://github.com/spoonconsulting/cordova-plugin-simple-camera-preview/compare/v2.0.21...v2.0.22) (2024-07-12) +* **iOS:** Release memory when exiting capture preview + ## [2.0.21](https://github.com/spoonconsulting/cordova-plugin-simple-camera-preview/compare/v2.0.20...v2.0.21) (2024-07-05) * **iOS:** Release memory consumption diff --git a/package-lock.json b/package-lock.json index 508e4d4..6f89bd0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@spoonconsulting/cordova-plugin-simple-camera-preview", - "version": "2.0.21", + "version": "2.0.22", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@spoonconsulting/cordova-plugin-simple-camera-preview", - "version": "2.0.21", + "version": "2.0.22", "license": "Apache 2.0", "devDependencies": {} } diff --git a/package.json b/package.json index d25efa3..820a46e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spoonconsulting/cordova-plugin-simple-camera-preview", - "version": "2.0.21", + "version": "2.0.22", "description": "Cordova plugin that allows camera interaction from HTML code for showing camera preview below or on top of the HTML.", "keywords": [ "cordova", diff --git a/plugin.xml b/plugin.xml index b90ebbe..8cbc041 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ - + cordova-plugin-simple-camera-preview Cordova plugin that allows camera interaction from HTML code. Show camera preview popup on top of the HTML.