Reputation: 1304
I am adding an image to the first cell in a UITableView cellForRowAtIndexPath like so:
if (row == 0) {
UIImage *image = [UIImage imageNamed:@"numberOneIcon.png"];
//create a framework for the ui image view
CGRect frame = CGRectMake(10, 37, image.size.width, image.size.height);
//initialize the ui image view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = image;
[cell.contentView addSubview:imageView];
[image release];
//[imageView release];
}
If I uncomment out the [imageView release]; it will crash when you 'close' the app (press the home bttn) then return to the app and return to the view with the UITableView in it.
If I leave this commented it doesn't crash but am I not leaking memory here? If so is there another way to do this so as not to leak.
Many thanks
Upvotes: 1
Views: 270
Reputation: 10172
You can also use:
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:frame] autorelease];
for your convenience,
Upvotes: 0
Reputation: 12194
You should be releasing imageView rather than image. Change that code to the following:
[cell.contentView addSubview:imageView];
//[image release];
[imageView release];
You do not own image as you did not create it with alloc, new, nor did you retain it, thus you are not supposed to be releasing it. You did alloc an instance of imageView however, so that is what you release. I strongly recommend reading this.
Upvotes: 2