tags:

views:

3124

answers:

4

Here's my case: I have a table view showing contacts. Add button in the navigation bar is used to load another view for data entry. This new view has images in table header, and each table cell has either a UITextField or a UITextView. When i press Cancel, the view is popped out and memory is released.

Here's my issue: When i open the "Add" interface, provide a value in any of the UITextField or UITextView, and press "Cancel" to to go back to the parent view, i get an *EXC_BAD_ACCESS* error. When i trace it, the "Add Controller" calls dealloc properly, but after the [super dealloc] when i press Continue, it throws this error. Just this, no trace, although i'm using NSZombieEnabled. When i run the code with Instruments, i don't get any error :(

I hope i'm clear in explaining the issue. Any pointers? Thanks.

+1  A: 

The problem magically disappears when i remove [tableView release] from my "Add Contact" Controller class's dealloc method. But i should release the tableView object, right. Although i'm using nib file - and made tableView an IBOutlet.

Mustafa
No, you shouldn't. The rule to remember is 'If you didn't explicitly alloc, copy or retain it, then you don't explicitly release it.'
Boaz Stuller
+4  A: 

The most common cause of this error is when you release an object and some other mechanism tries to access/release/dealloc it later.

Whenever I get a report of an EXC_BAD_ACCESS error, my first recommendation is to step through the code to determine which line is causing it, and then to search for any explicit [object release] calls that reference that object. Comment them out one-by-one to find where you may have gone wrong (and, of course, make sure the object is properly released later).

If the line doesn't help you figure out which object(s) is/are causing the problem, start looking through your [object release] calls, and make sure you aren't releasing objects too many times by accident, or releasing objects you don't own.

This leads to a good general guideline regarding release in Objective-C:

If you own an object (allocate or retain it), you release it. If you don't own it (came via convenience method or someone else allocated it), you don't release it.

(Via Memory Management with Objective C / Cocoa / iPhone, which also has some good tips.)

breakingobstacles
+1  A: 

The general rule of thumb is that anything you create, you need to manage (retain/release). Anything that comes out of a NIB, should generally not be released in code.

Shawn Craver
A: 

Put the [super dealloc] as the last statement of the class dealloc method. You'll get this behavior when you dealloc the super class first.

Robert Signore