Skip to content

Commit

Permalink
Deallocate unused resource in an orderly manner
Browse files Browse the repository at this point in the history
  • Loading branch information
parveshneedhoo committed Jul 10, 2024
1 parent d15b117 commit d586287
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/ios/CameraRenderController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
CVOpenGLESTextureRef _lumaTexture;
}

-(void) deallocateRenderMemory;

@property (nonatomic) CameraSessionManager *sessionManager;
@property (nonatomic) CIContext *ciContext;
@property (nonatomic) CIImage *latestFrame;
Expand Down
68 changes: 41 additions & 27 deletions src/ios/CameraRenderController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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, ^{
Expand Down Expand Up @@ -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;
Expand All @@ -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
1 change: 1 addition & 0 deletions src/ios/CameraSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ - (void)deallocSession {
if (self.sessionQueue) {
self.sessionQueue = nil;
}
self.device = nil;
}

@end
7 changes: 4 additions & 3 deletions src/ios/SimpleCameraPreview.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d586287

Please sign in to comment.