Working with tools the other day I ran with a memory leak into a very simple code example as follows:
@IBAction func shareSheet(sender: AnyObject) {
let firstActivityItem = "Hello there is a memory leak here..."
let secondActivityItem : NSURL = NSURL(string: "http://www.sample.com/")!
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = (sender as! UIButton)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
The purpose of the above code is to show a UIActivityViewControllermessage on a social network, etc.
Tools leak screenshots :


Working on the problem, I finally decided to change the declaration of the two constants firstActivityItemand secondActivityItemvariables, a memory leak disappeared.
But my question is: why is this?
Why generate a memory leak with let, rather than with an ad var?
source
share