iosuitableviewswiftmemory-leakshaneke

iOS Haneke - memory warning then crash


enter image description here

I am developing an app that fetches images from Twitter using the Haneke library. Once I get the images, I display them using a UITableView. I fetch 10 pictures at a time. My app loads but quickly crashes due to a memory leak.

The code I am using to set the images is as follows.

var tweetImage = "http://pbs.twimg.com/media/CGe89eqWQAACBxR.jpg"

if let var urlString = tweetImage {
  var url = NSURL(string: urlString)
  cell.tweetImage?.sizeToFit()
  cell.tweetImage?.hnk_setImageFromURL(url!)
}

When I comment out the last line cell.tweetImage?.hnk_setImageURL(url!) I no longer receive the memory warnings and it does not crash.

This is the first app I have ever worked on or made, what is the best way to go about fixing this memory problem? Or am I possibly using the Haneke library wrong?

Thanks in advance, above is the output from Instruments if that helps.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell= tableView.dequeueReusableCellWithIdentifier("tweetCell", forIndexPath: indexPath) as! TweetTableViewCell

    if (indexPath.row % 2 == 0){
        cell.backgroundColor = UIColor.whiteColor()
    }else{
        cell.backgroundColor = UIColor(red: 0.973, green: 0.973, blue: 0.973, alpha: 1)
    }

    var row = self.results[indexPath.row]

    var text = row["text"].string
    var name = row["name"].string
    var image = row["image"].string
    var avatar = row["avatar"].string
    var votes = row["rank"].int
    var long = row["long"].string
    var lat = row["lat"].string
    var id = row["id"].int

    var tweetImage = image == nil ? avatar : image

    if let urlString = tweetImage {
        var url = NSURL(string: urlString)
        cell.tweetImage?.sizeToFit()
        cell.tweetImage?.hnk_setImageFromURL(url!)
    }

    return cell
}

Above is my cellForRowAtIndexPath method, so far I am just working with the images. self.results is loaded from an api with the AlamoFire Library


Solution

  • I removed cell.tweetImage?.sizeToFit() from the original code and now it works. Not sure why I had that code in there in the first place but removing it resolved the memory leak issue. If anyone knows why the misuse of cell.tweetImage?.sizeToFit() in this context would cause a memory leak I would love to know.

    Correct code:

    var tweetImage = "http://pbs.twimg.com/media/CGe89eqWQAACBxR.jpg"
    
    if let var urlString = tweetImage {
     var url = NSURL(string: urlString)
     cell.tweetImage?.hnk_setImageFromURL(url!)
    }