One of the views od my app shows a list of images. When I scroll that list several times then my app crashes. I profiled it with instrument and it seems that the cells of the list are taking more memory as the list is scrolled.
Should a custom UITableCell be 'autoreleased' when returned from tableView:cellForRowAtIndexPath: ? (if I do, I have a crash on iOS 4.3 / it's fine in iOS 5.0 and 6.1)
This custom UITableCell have several pictures drawn into its 'contentView'. Those pictures are actually custom UIButton in which I set the background image.
The images are managed with HJManagedImageV
code for custom UIButton :
@implementation ProductTileButtonIpad
@synthesize product;
- (id)initWithFrame:(CGRect)frame andProduct:(Product *)aProduct {
if (self = [super initWithFrame:frame]) {
self.product = aProduct;
self.productTileView = [[[HJManagedImageV alloc] initWithFrame:self.frame] autorelease];
self.productTileView.callbackOnSetImage = self;
self.productTileView.url = some picture url
[[ImageManager instance] manage:self.productTileView];
}
return self;
}
#pragma mark -
#pragma mark HJManagedImageV delegate
-(void) managedImageSet:(HJManagedImageV*)mi {
[self setBackgroundImage:mi.image forState:UIControlStateNormal];
}
-(void) managedImageCancelled:(HJManagedImageV*)mi {
}
- (void)dealloc {
NSLog(@"deallocating ProductTileButtonIpad");
[self.product release];
[self.productTileView release];
[super dealloc];
}
@end
code for the custom cell
@implementation ProductGridCellIpad
@synthesize products, parentController;
- (void)initializeWithProducts:(NSMutableArray *)productsToShow{
self.products = productsToShow;
// clear possible old subviews
for (UIView *v in self.contentView.subviews) {
[v removeFromSuperview];
}
NSInteger width = 240;
NSInteger height = 240;
Product *product0 = [products objectAtIndex:0];
self.productTile0 = [[[ProductTileButtonIpad alloc] initWithFrame:CGRectMake(12, 12, width, height) andProduct:product0] autorelease];
[self.productTile0 addTarget:self.parentController action:@selector(selectedProduct:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.productTile0];
[self.productTile0 release];
if ([self.products count] > 1) {
Product *product1 = [products objectAtIndex:1];
self.productTile1 = [[[ProductTileButtonIpad alloc] initWithFrame:CGRectMake(12 + width + 12, 12, width, height) andProduct:product1] autorelease];
[self.productTile1 addTarget:self.parentController action:@selector(selectedProduct:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.productTile1];
[self.productTile1 release];
}
if ([self.products count] > 2) {
Product *product2 = [products objectAtIndex:2];
self.productTile2 = [[[ProductTileButtonIpad alloc] initWithFrame:CGRectMake(2*(12 + width) + 12, 12, width, height) andProduct:product2] autorelease];
[self.productTile2 addTarget:self.parentController action:@selector(selectedProduct:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.productTile2];
[self.productTile2 release];
}
}
- (void)dealloc {
NSLog(@"deallocating ProductGridCellIpad");
if(self.products)
[self.products release];
if(self.productTile0)
[self.productTile0 release];
if(self.productTile1)
[self.productTile1 release];
if(self.productTile2)
[self.productTile2 release];
[super dealloc];
}
@end
and here's the code that creates the cell :
NSString *productGridCellIpadIdentifier = @"ProductGridCellIpadIdentifier";
ProductGridCellIpad *cell = [tableView dequeueReusableCellWithIdentifier:productGridCellIpadIdentifier];
if(cell == nil) {
cell = [[ProductGridCellIpad alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:productGridCellIpadIdentifier];
[cell setFrame:CGRectMake(0, 0, self.view.frame.size.width, 244)];
}
[cell setParentController:self];
[cell initializeWithProducts:products];
return cell;
As it is now the code crashes right away on iOS 4.3. It works on iOS 5 and 6 but the app still crashes after a certain time of use/scrolling the table.
I don't use ARC.
I added some NSLog in the dealloc methods to see what's happening and I can see lots of "deallocating ProductTileButtonIpad" but I never see "deallocating ProductGridCellIpad"
My app easily reaches memory usage of 400Mb.
What am I doing wrong here?
If some of you have any thoughts, ideas that could help my understanding it would be much appreciated :)