views:

39

answers:

2

What will happen if the autorelease is removed from cell creation in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    

STVCell *cell = (STVCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[STVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
}
+1  A: 

Nothing. And that's a bad thing: The cell will never be released and become a memory leak.

Nikolai Ruhe
in this case, where would I manually release it?
Sheehan Alam
+4  A: 

It will lead to a memory leak.

Because you call alloc, you are responsible for also calling release (or in this case autorelease).

The UITableView will automatically retain the cell and release its use of the cell at the appropriate time, but unless your code also releases the reference that it holds, the cell can never be deallocated.

David Gelhar
in this case, where would I manually release it?
Sheehan Alam
There is no safe place to manually release it, which is why you need to use autorelease. You can't release it before you return it (because it would be deallocated before your caller gets a chance to see it), and once you've returned, there's no way for you to know when it's "safe" to call release. (Not to mention, where would you store the reference so you know what to release?) This situation is exactly what `autorelease` is designed for: when a method needs to return a reference to allocated storage and release it "soon". Use autorelease.
David Gelhar