loadView() throwing EXC_BAD_ACCESS memory error

252 Views Asked by At

I am pushing a ViewController onto the stack when a row is selected from a tableview:

if let cell = tableView.cellForRowAtIndexPath(indexPath){

        let genre = cell.textLabel?.text ?? SelectGenreTableViewController.genres[0]; // nil coalsing trtary operand, if text desnt exist assign first value or static array belining to class not instance
        let vc = AddCommentsViewController();
        vc.genre = genre;
        navigationController?.pushViewController(vc, animated: true);

This new view controller view is built programmatically in loadView():

 override func loadView() {
        // pin the text voew to all sides and use dynamoc to make font size adjustable to user

        comments = UITextView(); // BAD ACCESS THROWN HERE
        comments.translatesAutoresizingMaskIntoConstraints = false;
        comments.delegate = self;
        comments.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody);
        view.addSubview(comments);



 view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[comments]|", options: .AlignAllCenterX, metrics: nil, views: ["comments": comments]))
 view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[comments]|", options: .AlignAllCenterX, metrics: nil, views: ["comments": comments]))
 }

The issue is I get a :

Thread 1: EXC_BAD_ACCESS ....'

error when the comments textview is initialised in line 1 of loadView().

By debugging I have noted the loadMethod() gets called over and over again and eventually the app runs out of memory, hence the error.

Any idea why loadView() would be called continouly?

Thanks

1

There are 1 best solutions below

0
dancingbush On BEST ANSWER

Forgot to call super.loadView();