Skip to content

Commit 237d173

Browse files
committed
fix mem leak on ios
a closure was breaking the weekSelf reference approach a CFTimer target was retaining self (fixed by invalidate)
1 parent e26dcb2 commit 237d173

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

ios/IPDFCameraViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef NS_ENUM(NSInteger, IPDFRectangeType)
4242

4343
@property (nonatomic,assign) IPDFCameraViewType cameraViewType;
4444

45-
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandler;
45+
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)(void))completionHandler;
4646

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

ios/IPDFCameraViewController.m

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88

99
#import "IPDFCameraViewController.h"
1010

11+
#import <React/RCTInvalidating.h>
1112
#import <AVFoundation/AVFoundation.h>
1213
#import <CoreMedia/CoreMedia.h>
1314
#import <CoreVideo/CoreVideo.h>
1415
#import <CoreImage/CoreImage.h>
1516
#import <ImageIO/ImageIO.h>
1617
#import <GLKit/GLKit.h>
1718

18-
@interface IPDFCameraViewController () <AVCaptureVideoDataOutputSampleBufferDelegate>
19+
@interface IPDFCameraViewController () <AVCaptureVideoDataOutputSampleBufferDelegate, RCTInvalidating>
1920

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

30+
@property (atomic, assign) BOOL isCapturing;
31+
@property (atomic, assign) CGFloat imageDetectionConfidence;
32+
2933
@end
3034

3135
@implementation IPDFCameraViewController
@@ -36,12 +40,9 @@ @implementation IPDFCameraViewController
3640

3741
BOOL _isStopped;
3842

39-
CGFloat _imageDedectionConfidence;
4043
NSTimer *_borderDetectTimeKeeper;
4144
BOOL _borderDetectFrame;
4245
CIRectangleFeature *_borderDetectLastRectangleFeature;
43-
44-
BOOL _isCapturing;
4546
}
4647

4748
- (void)awakeFromNib
@@ -63,6 +64,11 @@ - (void)_foregroundMode
6364
self.forceStop = NO;
6465
}
6566

67+
- (void)invalidate
68+
{
69+
[self stop];
70+
}
71+
6672
- (void)dealloc
6773
{
6874
[[NSNotificationCenter defaultCenter] removeObserver:self];
@@ -106,7 +112,7 @@ - (void)setupCameraView
106112
}
107113
if (!device) return;
108114

109-
_imageDedectionConfidence = 0.0;
115+
self.imageDetectionConfidence = 0.0;
110116

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

171177
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
172178

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

192198
if (_borderDetectLastRectangleFeature)
193199
{
194-
_imageDedectionConfidence += .5;
200+
self.imageDetectionConfidence += .5;
195201

196202
image = [self drawHighlightOverlayForPoints:image topLeft:_borderDetectLastRectangleFeature.topLeft topRight:_borderDetectLastRectangleFeature.topRight bottomLeft:_borderDetectLastRectangleFeature.bottomLeft bottomRight:_borderDetectLastRectangleFeature.bottomRight];
197203
}
198204
else
199205
{
200-
_imageDedectionConfidence = 0.0f;
206+
self.imageDetectionConfidence = 0.0f;
201207
}
202208
}
203209

@@ -307,7 +313,7 @@ - (void)setDetectionRefreshRateInMS:(NSInteger)detectionRefreshRateInMS
307313
}
308314

309315

310-
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandler
316+
- (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)(void))completionHandler
311317
{
312318
AVCaptureDevice *device = self.captureDevice;
313319
CGPoint pointOfInterest = CGPointZero;
@@ -341,9 +347,10 @@ - (void)focusAtPoint:(CGPoint)point completionHandler:(void(^)())completionHandl
341347
}
342348
}
343349

344-
- (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRectangleFeature *rectangleFeature))completionHandler
350+
- (void)captureImageWithCompletionHander:(void(^)(UIImage *data, UIImage *initialData, CIRectangleFeature *rectangleFeature))completionHandler;
345351
{
346-
if (_isCapturing) return;
352+
if (self.isCapturing) return;
353+
self.isCapturing = true;
347354

348355
__weak typeof(self) weakSelf = self;
349356

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

358-
_isCapturing = YES;
359-
360365
AVCaptureConnection *videoConnection = nil;
361366
for (AVCaptureConnection *connection in self.stillImageOutput.connections)
362367
{
@@ -388,7 +393,7 @@ - (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRec
388393
enhancedImage = [self filteredImageUsingContrastFilterOnImage:enhancedImage];
389394
}
390395

391-
if (weakSelf.isBorderDetectionEnabled && rectangleDetectionConfidenceHighEnough(_imageDedectionConfidence))
396+
if (weakSelf.isBorderDetectionEnabled && rectangleDetectionConfidenceHighEnough(weakSelf.imageDetectionConfidence))
392397
{
393398
CIRectangleFeature *rectangleFeature = [self biggestRectangleInRectangles:[[self highAccuracyRectangleDetector] featuresInImage:enhancedImage]];
394399

@@ -419,15 +424,15 @@ - (void)captureImageWithCompletionHander:(void(^)(id data, id initialData, CIRec
419424
completionHandler(initialImage, initialImage, nil);
420425
}
421426

422-
_isCapturing = NO;
427+
weakSelf.isCapturing = NO;
423428
}];
424429
}
425430

426-
- (void)hideGLKView:(BOOL)hidden completion:(void(^)())completion
431+
- (void)hideGLKView:(BOOL)hidden completion:(void(^)(void))completion
427432
{
428433
[UIView animateWithDuration:0.1 animations:^
429434
{
430-
_glkView.alpha = (hidden) ? 0.0 : 1.0;
435+
self->_glkView.alpha = (hidden) ? 0.0 : 1.0;
431436
}
432437
completion:^(BOOL finished)
433438
{

0 commit comments

Comments
 (0)