From 36b618c03decfb0056178e4c38ddcdd54345a8f3 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sun, 4 Apr 2021 12:34:19 -0700 Subject: [PATCH] Ensure no memory leaks caused by image download completion blocks --- View Controllers/EditAttachmentViewController.swift | 8 ++++---- View Controllers/ImageViewController.swift | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/View Controllers/EditAttachmentViewController.swift b/View Controllers/EditAttachmentViewController.swift index 5f2d34d..06cdbb5 100644 --- a/View Controllers/EditAttachmentViewController.swift +++ b/View Controllers/EditAttachmentViewController.swift @@ -190,13 +190,13 @@ private extension EditAttachmentViewController { SDWebImageManager.shared.loadImage( with: viewModel.attachment.url.url, options: [], - progress: nil) { image, _, _, _, _, _ in - guard let cgImage = image?.cgImage else { return } + progress: nil) { [weak self] image, _, _, _, _, _ in + guard let self = self, let cgImage = image?.cgImage else { return } self.detectText(cgImage: cgImage) - .sink { [weak self] in + .sink { if case let .failure(error) = $0 { - self?.present(alertItem: .init(error: error)) + self.present(alertItem: .init(error: error)) } } receiveValue: { [weak self] in guard let self = self else { return } diff --git a/View Controllers/ImageViewController.swift b/View Controllers/ImageViewController.swift index a24c7a9..374806c 100644 --- a/View Controllers/ImageViewController.swift +++ b/View Controllers/ImageViewController.swift @@ -140,11 +140,11 @@ final class ImageViewController: UIViewController { } imageView.sd_setImage(with: viewModel.attachment.url.url, - placeholderImage: placeholderImage) { _, error, _, _ in + placeholderImage: placeholderImage) { [weak self] _, error, _, _ in if error != nil { let alertItem = AlertItem(error: ImageError.unableToLoad) - self.present(alertItem: alertItem) + self?.present(alertItem: alertItem) } } case .gifv: