Skip to content

Commit

Permalink
fix mem leak on ios
Browse files Browse the repository at this point in the history
a closure was breaking the weekSelf reference approach
a CFTimer target was retaining self (fixed by invalidate)
  • Loading branch information
Tweenkie committed Dec 18, 2019
1 parent e26dcb2 commit 237d173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ios/IPDFCameraViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef NS_ENUM(NSInteger, IPDFRectangeType)

@property (nonatomic,assign) IPDFCameraViewType cameraViewType;

- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandler;
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)(void))completionHandler;

- (void)captureImageWithCompletionHander:(void(^)(UIImage *data, UIImage *initialData, CIRectangleFeature *rectangleFeature))completionHandler;

Expand Down
39 changes: 22 additions & 17 deletions ios/IPDFCameraViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

#import "IPDFCameraViewController.h"

#import <React/RCTInvalidating.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>
#import <CoreVideo/CoreVideo.h>
#import <CoreImage/CoreImage.h>
#import <ImageIO/ImageIO.h>
#import <GLKit/GLKit.h>

@interface IPDFCameraViewController () <AVCaptureVideoDataOutputSampleBufferDelegate>
@interface IPDFCameraViewController () <AVCaptureVideoDataOutputSampleBufferDelegate, RCTInvalidating>

@property (nonatomic,strong) AVCaptureSession *captureSession;
@property (nonatomic,strong) AVCaptureDevice *captureDevice;
Expand All @@ -26,6 +27,9 @@ @interface IPDFCameraViewController () <AVCaptureVideoDataOutputSampleBufferDele
@property (nonatomic, assign) BOOL forceStop;
@property (nonatomic, assign) float lastDetectionRate;

@property (atomic, assign) BOOL isCapturing;
@property (atomic, assign) CGFloat imageDetectionConfidence;

@end

@implementation IPDFCameraViewController
Expand All @@ -36,12 +40,9 @@ @implementation IPDFCameraViewController

BOOL _isStopped;

CGFloat _imageDedectionConfidence;
NSTimer *_borderDetectTimeKeeper;
BOOL _borderDetectFrame;
CIRectangleFeature *_borderDetectLastRectangleFeature;

BOOL _isCapturing;
}

- (void)awakeFromNib
Expand All @@ -63,6 +64,11 @@ - (void)_foregroundMode
self.forceStop = NO;
}

- (void)invalidate
{
[self stop];
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
Expand Down Expand Up @@ -106,7 +112,7 @@ - (void)setupCameraView
}
if (!device) return;

_imageDedectionConfidence = 0.0;
self.imageDetectionConfidence = 0.0;

AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.captureSession = session;
Expand Down Expand Up @@ -166,7 +172,7 @@ - (void)setCameraViewType:(IPDFCameraViewType)cameraViewType
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (self.forceStop) return;
if (_isStopped || _isCapturing || !CMSampleBufferIsValid(sampleBuffer)) return;
if (_isStopped || self.isCapturing || !CMSampleBufferIsValid(sampleBuffer)) return;

CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);

Expand All @@ -191,13 +197,13 @@ -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMS

if (_borderDetectLastRectangleFeature)
{
_imageDedectionConfidence += .5;
self.imageDetectionConfidence += .5;

image = [self drawHighlightOverlayForPoints:image topLeft:_borderDetectLastRectangleFeature.topLeft topRight:_borderDetectLastRectangleFeature.topRight bottomLeft:_borderDetectLastRectangleFeature.bottomLeft bottomRight:_borderDetectLastRectangleFeature.bottomRight];
}
else
{
_imageDedectionConfidence = 0.0f;
self.imageDetectionConfidence = 0.0f;
}
}

Expand Down Expand Up @@ -307,7 +313,7 @@ - (void)setDetectionRefreshRateInMS:(NSInteger)detectionRefreshRateInMS
}


- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandler
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)(void))completionHandler
{
AVCaptureDevice *device = self.captureDevice;
CGPoint pointOfInterest = CGPointZero;
Expand Down Expand Up @@ -341,9 +347,10 @@ - (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandl
}
}

- (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRectangleFeature *rectangleFeature))completionHandler
- (void)captureImageWithCompletionHander:(void(^)(UIImage *data, UIImage *initialData, CIRectangleFeature *rectangleFeature))completionHandler;
{
if (_isCapturing) return;
if (self.isCapturing) return;
self.isCapturing = true;

__weak typeof(self) weakSelf = self;

Expand All @@ -355,8 +362,6 @@ - (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRec
}];
}];

_isCapturing = YES;

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.stillImageOutput.connections)
{
Expand Down Expand Up @@ -388,7 +393,7 @@ - (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRec
enhancedImage = [self filteredImageUsingContrastFilterOnImage:enhancedImage];
}

if (weakSelf.isBorderDetectionEnabled && rectangleDetectionConfidenceHighEnough(_imageDedectionConfidence))
if (weakSelf.isBorderDetectionEnabled && rectangleDetectionConfidenceHighEnough(weakSelf.imageDetectionConfidence))
{
CIRectangleFeature *rectangleFeature = [self biggestRectangleInRectangles:[[self highAccuracyRectangleDetector] featuresInImage:enhancedImage]];

Expand Down Expand Up @@ -419,15 +424,15 @@ - (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRec
completionHandler(initialImage, initialImage, nil);
}

_isCapturing = NO;
weakSelf.isCapturing = NO;
}];
}

- (void)hideGLKView:(BOOL)hidden completion:(void(^)())completion
- (void)hideGLKView:(BOOL)hidden completion:(void(^)(void))completion
{
[UIView animateWithDuration:0.1 animations:^
{
_glkView.alpha = (hidden) ? 0.0 : 1.0;
self->_glkView.alpha = (hidden) ? 0.0 : 1.0;
}
completion:^(BOOL finished)
{
Expand Down

0 comments on commit 237d173

Please sign in to comment.