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
I have heard that the instrument's leak tool can report false leaks.
The memory seems to not go up:
The problem seems to be in UIImage, see Instruments Leak report:
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?
I have the issue too.
Just create clean new project to reproduce the issue.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "image", ofType: "png")!
let _ = UIImage(contentsOfFile: path)
}
}
Not sure is it a bug of init(contentsOfFile:)
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With