0

While reloading a tableView the app crashes and I am getting error malloc:

error for object 0x7f8c7b99be80: pointer being freed was not allocated set a breakpoint in malloc_error_break to debug

I have set the Symbolic breakpoint for malloc_error_break

Also try to find memory leakage with "Instruments" but it just showing same error in console but here is no any memory leakage. How to solve this issue.

(I am using 8.3 SDK and 6.3.1 Xcode)

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var ResultCellIdentifier = "placementID"
    var LoadCellIdentifier = "LoadingCell"

    var count = self.arrayOfAllData.count as NSInteger
    if count == 0 && indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("LoadingCell") as!  UITableViewCell
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCellWithIdentifier("placementID") as!  PlacementsTableViewCell

        indexPATH = indexPath
        var tweet = self.arrayOfAllData.objectAtIndex(indexPath.row) as! NSDictionary

        let created_at = tweet.valueForKey("created_at") as? NSString
        var dateArray : NSArray = created_at!.componentsSeparatedByString(" ")
        if dateArray.count != 0 {
            let month = dateArray.objectAtIndex(1) as!  String
            let date = dateArray.objectAtIndex(2) as!  String

            cell.timeLabel.text = String(format: "%@ %@", date,month)
        }


        ///////////any url present in tweet text
        var entities = NSDictionary()
        entities = tweet.valueForKey("entities") as! NSDictionary

        var urlsArray = entities.valueForKey("urls") as! NSArray
        if urlsArray.count == 0 {

        }else {

            for item in urlsArray as! [NSDictionary] {
                let expanded_url = item.valueForKey("expanded_url") as? String
            }
        }
        ///////////

        var tweet_id_str = NSString()
        var user_id_str = NSString()
        var data = NSData()
        //            var name = NSString()
        ///////////count of retweets
        var retweet_count = tweet.valueForKey("retweet_count") as!  NSInteger

        var retweeted_status = tweet.valueForKey("retweeted_status") as! NSDictionary
        var favorite_count = retweeted_status.valueForKey("favorite_count") as!  NSInteger
        ///////////tweet id
        tweet_id_str = retweeted_status.valueForKey("id_str") as!  NSString

        if retweeted_status.isEqual(nil) {

        }
        else {
            var user = retweeted_status.valueForKey("user") as! NSDictionary
            ///////////last update
            let created_at = retweeted_status.valueForKey("created_at") as! NSString
            ///////////

            ///////////user name who added this tweet
            cell.titleLabel.text = user.valueForKey("name") as? String
            ///////////user id who added this tweet
            user_id_str = user.valueForKey("id_str") as!  NSString
            ///////////screen name
            let screen_name = user.valueForKey("screen_name") as!  NSString

            var followers_count = user.valueForKey("followers_count") as!  NSInteger

            ///////////profile image of the tweet

            cell.avatarImageView.image = UIImage(named: "Twitter Avatar.jpg")

            let profile_image_url = user.valueForKey("profile_image_url") as! NSString
            var imageUrl = NSURL(string: profile_image_url as String)

            let request: NSURLRequest = NSURLRequest(URL: imageUrl!)

            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                if error == nil {

                    if data != nil {
                        var image = UIImage(data: data)
                        cell.avatarImageView.image = image
                    }
                }
            })


            if retweet_count >= 5 ||  favorite_count >= 15 || followers_count >= 30000 {
                cell.featuredImageView.hidden = false
                cell.featuredImageView.image = UIImage(named: "new feature star")
            }
            else {
                cell.featuredImageView.hidden = true
            }

        }

        cell.mailLabel.text = ""

        let tweetText = tweet.valueForKey("text") as? NSString
        cell.beattypeLabel.text = tweetText as? String

        if tweetText?.containsString("@gmail") == true{
            NSLog("Mail id is present at index : %d", indexPath.row)
            var words = NSArray()
            words = NSArray(array: tweetText!.componentsSeparatedByString(" "))
            var mailAddress = NSString()
            for var i = 0; i < words.count; i++ {
                var mailAdd = words.objectAtIndex(i) as!  NSString

                if mailAdd.rangeOfString("@gmail").location != NSNotFound {
                    NSLog("mail Address : %@", mailAdd)
                    if mailAdd.rangeOfString(".com").location == NSNotFound {
                        var lastChar = mailAdd.characterAtIndex(mailAdd.length-1)
                        var lastCharStr:NSString = NSString(format: "%ch", lastChar)

                        mailAddress = mailAdd.stringByAppendingString(".com")
                    }
                }
            }
        }


        return cell
    }        
}

at the same time Thread 1 showing this https://i.stack.imgur.com/2YoQp.jpg (please go through this image)

dario
  • 5,149
  • 12
  • 28
  • 32

1 Answers1

0

use

var cell = tableView.dequeueReusableCellWithIdentifier("placementID") as! PlacementsTableViewCell
if cell.isEqual(nil) {
    cell = PlacementsTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "placementID")
}

instead of

let cell = tableView.dequeueReusableCellWithIdentifier("placementID") as!  PlacementsTableViewCell

and change downloading image in other thread like follow

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
     println("This is run on the background queue")
     var imageData = NSData(contentsOfURL: imageUrl!)
     var image = UIImage(data: imageData!)
     dispatch_async(dispatch_get_main_queue(), { () -> Void in
          println("This is run on the main queue, after the previous code in outer block")
          cell.avatarImageView.image = image
      })
 })