Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak and remove notification when QRCodeReaderView deinit #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/QRCodeReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegat
}
}

deinit {
print("QRCodeReader --- deinit")
}
// MARK: - Initializing the AV Components

private func configureDefaultComponents(withCaptureDevicePosition: AVCaptureDevice.Position) {
Expand Down
22 changes: 15 additions & 7 deletions Sources/QRCodeReaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,24 @@ final public class QRCodeReaderView: UIView, QRCodeReaderDisplayable {
}
}

deinit {
NotificationCenter.default.removeObserver(self, name: orientationNotificationName, object: nil)
print("QRCodeReaderView --- deinit")
}

private var orientationNotificationName: NSNotification.Name {
#if swift(>=4.2)
let notificationName = UIDevice.orientationDidChangeNotification
#else
let notificationName = NSNotification.Name.UIDeviceOrientationDidChange
#endif
return notificationName
}

// MARK: - Convenience Methods

private func addComponents() {
#if swift(>=4.2)
let notificationName = UIDevice.orientationDidChangeNotification
#else
let notificationName = NSNotification.Name.UIDeviceOrientationDidChange
#endif

NotificationCenter.default.addObserver(self, selector: #selector(self.setNeedsUpdateOrientation), name: notificationName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.setNeedsUpdateOrientation), name: orientationNotificationName, object: nil)

addSubview(cameraView)

Expand Down
8 changes: 4 additions & 4 deletions Sources/QRCodeReaderViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ public class QRCodeReaderViewController: UIViewController {

view.backgroundColor = .black

codeReader.didFindCode = { [weak self] resultAsObject in
codeReader.didFindCode = { [weak self, weak builder] resultAsObject in
if let weakSelf = self {
if let qrv = builder.readerView.displayable as? QRCodeReaderView {
if let qrv = builder?.readerView.displayable as? QRCodeReaderView {
qrv.addGreenBorder()
}
weakSelf.completionBlock?(resultAsObject)
weakSelf.delegate?.reader(weakSelf, didScanResult: resultAsObject)
}
}

codeReader.didFailDecoding = {
if let qrv = builder.readerView.displayable as? QRCodeReaderView {
codeReader.didFailDecoding = { [weak builder] in
if let qrv = builder?.readerView.displayable as? QRCodeReaderView {
qrv.addRedBorder()
}
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/QRCodeReaderViewControllerBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public final class QRCodeReaderViewControllerBuilder {
*/
public init() {}

deinit {
print("QRCodeReaderViewControllerBuilder --- deinit")
}
/**
Initialize a QRCodeReaderViewController builder with default values.

Expand Down