UIImageView instances of NSPathStore2 leaked
Asked Answered
C

2

6

I am loading png images from the Document directory into an UIImageView. When using the xcode Debug memory graph it comes up with Memory issues: instances of NSPathStore2 leaked instances of NSPathStore2 leaked

I have heard that the instrument's leak tool can report false leaks.

The memory seems to not go up: enter image description here

The problem seems to be in UIImage, see Instruments Leak report: enter image description here

Here is the code that produces the images:

           try FileManager.default.copyfileToUserDocumentDirectory(forResource: "smiley", ofType: ".png")

            var fullPathString = ""
            if let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                                                .userDomainMask,
                                                                true).first {
                let fileName = "smiley.png"
                let fullDestPath = URL(fileURLWithPath: docDir).appendingPathComponent(fileName)
                fullPathString = fullDestPath.path
            }

            mainStackView.addArrangedSubview(UIImageView(image: UIImage(contentsOfFile: fullPathString)))
            mainStackView.addArrangedSubview(UIImageView(image: UIImage(contentsOfFile: fullPathString)))


The working project producing this problem can be found here: UIImageViewNSPathStore2leaked

Is there a way to change the code so that these leaks go away, or is this one of those false reports?

Cagle answered 16/5, 2020 at 5:54 Comment(0)
D
8

I have the issue too.

Reproduce

Just create clean new project to reproduce the issue.

  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.

Not sure is it a bug of init(contentsOfFile:)?

Solution

I changed the API init(data:) instead of init(contentsOfFile:) to solve the problem.

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

Deign answered 28/5, 2020 at 9:45 Comment(2)
I have the same question, But why? instances of NSPathStore2 leaked is caused by init(contentsOfFile:)Backspin
Seems like a bug in Instruments, perhaps a false positive. In Xcode 12 beta 6 this no longer shows up as a leak.Ebro
E
-1

Seems like a bug in Instruments, perhaps a false positive. In Xcode 12 beta 6 this no longer shows up as a leak.

Just to note, in Xcode 11.6/11.7, the "leak" also happens with UIImage(named: ) method:

UIImage(named: ) leak.

Ebro answered 4/9, 2020 at 18:41 Comment(1)
Still happens in the latest XCode (12.5.1), and also crashing my app in productionDomel

© 2022 - 2024 — McMap. All rights reserved.