I would like to know how to correctly release a cell and fix a memory leak I have. and also ask if a memory leak can cause crashing on scrolling if the numbers cell numbers get high?
Here is my cell where the leak is coming from any suggestions?
NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
}
//add subtitle
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"cell"];
/*
//Here it adds a nice shadow to the table view but will crash on rotation and send a
wierd dump to ApplistViewController !!!????
tableView.layer.shadowColor = [[UIColor blackColor] CGColor];
tableView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
tableView.layer.shadowRadius = 8.0f;
tableView.layer.shadowOpacity = 1.0f;
*/
[cell.textLabel setNumberOfLines:1];
[cell.textLabel setText:[(Tweet*)[authors objectAtIndex:indexPath.row] author]];
[cell.detailTextLabel setText:[(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]];
[cell.detailTextLabel setNumberOfLines:10];
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.textLabel setShadowColor:[UIColor whiteColor]];
[cell.textLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
[cell.detailTextLabel setTextColor:[UIColor blackColor]];
//[cell.detailTextLabel setText:[(Tweet*)[retweetCount objectAtIndex:indexPath.row]
reTweetCount]];
[cell.textLabel setUserInteractionEnabled:YES];
[cell.contentView setMultipleTouchEnabled:YES];
// cell.text = [[NSString alloc] initWithFormat:@"Cell :%i", indexPath.row];
NSURL *url = [NSURL URLWithString:[(Tweet*)[avatarsURL
objectAtIndex:indexPath.row]avatarURL]];
NSData *data = [NSData dataWithContentsOfURL:url];
// NSLog(@"http://a0.twimg.com/profile_images/1268205919/iDD_normal.png");
UIImage *tableImage = [[UIImage alloc] initWithData:data];
[cell.imageView setImage:tableImage];
cell.imageView.image = tableImage ;
CGSize imageSize = CGSizeMake(45,45);
UIGraphicsBeginImageContext(imageSize);
CGRect imageRect = CGRectMake(0.2, 0.1, imageSize.width, imageSize.height);
[tableImage drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//PlaceMent avatars! would like to use a if statment to place this image in the cell if
before it retrives the avatar just like the method above for t开发者_如何转开发he profile!
/*
NSString *avatar = [[NSBundle mainBundle] pathForResource:@"avatar" ofType:@"png"];
UIImage *setAvatar = [[UIImage alloc] initWithContentsOfFile:avatar];
//now place avatar in cell
cell.imageView.image = setAvatar;
*/
//add gradient to cell
UIImage *gradient = [UIImage imageNamed:@"gradientcell2.png"];
UIImageView *cellimage = [[UIImageView alloc] initWithImage:gradient];
cellimage.contentMode = UIViewContentModeScaleToFill;
cell.backgroundView = cellimage;
[cellimage release];
UIImage *selectedGradient = [UIImage imageNamed:@"selectedcell.png"];
UIImageView *selectedCell = [[UIImageView alloc] initWithImage:selectedGradient];
selectedCell.contentMode = UIViewContentModeScaleToFill;
cell.selectedBackgroundView = selectedCell;
//subtitle if needed !! Also change UITableViewStlyle from default to Subtitle up 7
lines
// cell.detailTextLabel.text = @"subtile";
//yeah another warning within the tableview whats new =P
[tableView setBackgroundColor:[UIColor clearColor]];
return cell;
[cell autorelease];
}
ALL the LEAKS are fixed!!!! Thank You but why do I crash when I add a count integer ?
Any Suggestions?
2011-06-01 18:24:55.663 ThemeCatcher[4641:207] *** Terminating app due to uncaught
exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 16
beyond bounds [0 .. 15]'
*** Call stack at first throw:
(
0 CoreFoundation 0x014dcbe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x016315c2 objc_exception_throw + 47
2 CoreFoundation 0x014d26e5 -[__NSArrayM objectAtIndex:] +
261
3 ThemeCatcher 0x00025833 -[TwitterVeiwController
tableView:cellForRowAtIndexPath:] + 531
4 UIKit 0x007bb7fa -
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
5 UIKit 0x007b177f -
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
6 UIKit 0x007c6450 -
[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
7 UIKit 0x007be538 -[UITableView layoutSubviews] +
242
8 QuartzCore 0x00460451 -[CALayer layoutSublayers] + 181
9 QuartzCore 0x0046017c CALayerLayoutIfNeeded + 220
10 QuartzCore 0x0045937c
_ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
11 QuartzCore 0x004590d0 _ZN2CA11Transaction6commitEv +
292
12 QuartzCore 0x004897d5
_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
13 CoreFoundation 0x014bdfbb
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
14 CoreFoundation 0x014530e7 __CFRunLoopDoObservers + 295
15 CoreFoundation 0x0141bbd7 __CFRunLoopRun + 1575
16 CoreFoundation 0x0141b240 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x0141b161 CFRunLoopRunInMode + 97
18 GraphicsServices 0x01efa268 GSEventRunModal + 217
19 GraphicsServices 0x01efa32d GSEventRun + 115
20 UIKit 0x0075642e UIApplicationMain + 1160
21 ThemeCatcher 0x000021c9 main + 121
22 ThemeCatcher 0x00002145 start + 53
23 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Current language: auto; currently objective-c
(gdb)
cell
object needs to be autoreleased here –cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
and this line needs to be insideif ( cell == nil ) { //here }
.- You alloc-init
tableImage
but do not release it. selectedCell
is not released either.- You are
autorelease
ing after returning from the method. This won't happen and in case of reusable cell, it will send an additionalrelease
message crashing your app.
There seem to more issues within your comments which I have chosen not to include. Try using the Leaks
application to check for memory leaks.
when you write the below code in cellForRowAtIndex just autorelease it. your problem solved.
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellSeparatorStyleSingleLine reuseIdentifier:cellIdentifier] autorelease];
}
精彩评论