title: Memory leak, in do-catch block. iOS, Swift
body: <p>I am looking at memory leaks in my app that uses vision to detect text.</p>
  
  <p>I am getting a memory leak which when using the tree points to this line:</p>
  
  <pre><code>try imageRequestHandler.perform([self.textDetectionRequest])
  </code></pre>
  
  <p>I am not sure why and hope someone can help with this.</p>
  
  <p>Full code below.</p>
  
  <pre><code>private func performVisionRequest(image: CGImage, orientation: CGImagePropertyOrientation) {
  
      DispatchQueue.global(qos: .userInitiated).async {
          do {
              var imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
              try imageRequestHandler.perform([self.textDetectionRequest])
          } catch let error as NSError {
              print("Failed to perform vision request: \(error)")
  
          }
      }
  }
  </code></pre>
  
  <p>Here is the whole class:</p>
  
  <pre><code>import UIKit
  import Vision
  
  var noText: Bool!
  var imageNo: UIImage!
  
  internal class Slicer {
  
  private var image = UIImage()
  private var sliceCompletion: ((_ slices: [UIImage]) -&gt; Void) = { _ in }
  
  private lazy var textDetectionRequest: VNDetectTextRectanglesRequest = {
  
      return VNDetectTextRectanglesRequest(completionHandler: self.handleDetectedText)
  }()
  
  internal func slice(image: UIImage, completion: @escaping ((_: [UIImage]) -&gt; Void)) {
      self.image = image
      self.sliceCompletion = completion
      self.performVisionRequest(image: image.cgImage!, orientation: .up)
  }
  
  // MARK: - Vision
  
  private func performVisionRequest(image: CGImage, orientation: CGImagePropertyOrientation) {
  
      DispatchQueue.global(qos: .userInitiated).async {
          do {
              let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
              try imageRequestHandler.perform([self.textDetectionRequest])
          } catch let error as NSError {
              self.sliceCompletion([UIImage]())
              print("Failed to perform vision request: \(error)")
  
          }
      }
  }
  
  private func handleDetectedText(request: VNRequest?, error: Error?) {
      if let err = error as NSError? {
          print("Failed during detection: \(err.localizedDescription)")
          return
      }
      guard let results = request?.results as? [VNTextObservation], !results.isEmpty else {
  
          noText = true
          print("Tony no text found")
          var slices = [imageNo]
          self.sliceCompletion(slices as! [UIImage])
          slices = []
          return }
  
      noText = false
      self.sliceImage(text: results, onImageWithBounds: CGRect(x: 0, y: 0, width: self.image.cgImage!.width, height: self.image.cgImage!.height))
  }
  
  private func sliceImage(text: [VNTextObservation], onImageWithBounds bounds: CGRect) {
      CATransaction.begin()
  
      var slices = [UIImage]()
  
      for wordObservation in text {
          let wordBox = boundingBox(forRegionOfInterest: wordObservation.boundingBox, withinImageBounds: bounds)
  
          if !wordBox.isNull {
              guard let slice = self.image.cgImage?.cropping(to: wordBox) else { continue }
              slices.append(UIImage(cgImage: slice))
          }
      }
  
      self.sliceCompletion(slices)
  
      CATransaction.commit()
  }
  
  private func boundingBox(forRegionOfInterest: CGRect, withinImageBounds bounds: CGRect) -&gt; CGRect {
  
      let imageWidth = bounds.width
      let imageHeight = bounds.height
  
      // Begin with input rect.
      var rect = forRegionOfInterest
  
      // Reposition origin.
      rect.origin.x *= imageWidth
      rect.origin.y = ((1 - rect.origin.y) * imageHeight) - (forRegionOfInterest.height * imageHeight)
  
      // Rescale normalized coordinates. Tony adde + 30 to increase the size of rect
      rect.size.width *= imageWidth + 30
      rect.size.height *= imageHeight + 30
  
      return rect
  }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/nR3Py.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nR3Py.png" alt="enter image description here"></a></p>