Memory leak by UIImage init(contentsOfFile:)

I tried to load image from my app's sandbox path using

UIImage(contentsOfFile: path)
, but it causes memory leak issues by instances of NSPathStore2.


This is my testing example to reproduce it.


First, Create clean new project.

  1. Drag image.png to project, make sure it in Copy Bundle Resources.
  2. Write these two lines in viewDidLoad.
import UIKit

class ViewController: UIViewController {

  override func viewDidLoad() {
       super.viewDidLoad()
       let path = Bundle.main.path(forResource: "image", ofType: "png")!
       let _ = UIImage(contentsOfFile: path)
  }
}


  1. Build and Run it.
  2. Click Debug Memory Graph when viewController appeared.
  3. You can see leak issues by NSPathStore2.


Not sure is it a bug of

init(contentsOfFile:)
?


Now, I tried to load image by UIImage(data: imageData) likes below to avoid leaked issues


let url = Bundle.main.url(forResource: "image", withExtension: "png")!
let imageData = try! Data(contentsOf: url)
let _ = UIImage(data: imageData)

Using Xcode 11.5 / 11.3.1 and Swift 5

Does anyone know it is normal or bug issues?

This is a post link from stackoverflow.

I'm facing this issue in Xcode 11.7
Memory leak by UIImage init(contentsOfFile:)
 
 
Q