Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-25-2010, 11:49 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 3
Default Weird UITableView reloadData error

Hello, I was just finishing my application and I decided to check for some leaks. To my amazement two lines which did not used to have problems showed a lot of leaks:

Leak 1:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	if(debug)NSLog(@"LOG [%d]%s",__LINE__,__FUNCTION__);
	NSMutableDictionary *currentGame = [[[NSMutableArray alloc] init] autorelease];

	NSInteger increment = [self calculateIncrementAtIndexPath:indexPath];
	currentGame = [games objectAtIndex:(indexPath.row + increment)];
	
	NSString *CellIdentifier = @"CellGame";
	HangoverGameCell *cell = (HangoverGameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[HangoverGameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
	}
	
	[cell.labelName setText:[currentGame objectForKey:@"Name"]];
	[cell.imageType setImage:[UIImage imageNamed:[[types objectAtIndex:[[currentGame objectForKey:@"Type"] intValue]] objectForKey:@"Image"]]];
	[cell.imageRibbon setHidden:![[currentGame objectForKey:@"Popular"] boolValue]];
	[cell.imageLove setHidden:![[currentGame objectForKey:@"Love"] boolValue]];
	[cell.imageBeer setHidden:![[currentGame objectForKey:@"Beer"] boolValue]];

	return cell;
}
Leak 2:
Code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)theReuseIdentifier { 
	if (self = [super initWithStyle:style reuseIdentifier:theReuseIdentifier]) { 
		self = [[[NSBundle mainBundle] loadNibNamed:@"HangoverGameCell" owner:self options:nil] lastObject];
		self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gameCell.png"]];
		self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gameCellSelected.png"]];
	}
	return self; 
}
The leaking lines are in bold and red.

Obviously once I checked the lines and other codes that did this i changed

cell = [[HangoverGameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
to:
cell = [[[HangoverGameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]

which makes sense and fixes part of the leaking problem but now I have an error occurring each time I call: [tableview reloadData];

the error is the following
Code:
-[HangoverGameCell release]: message sent to deallocated instance 0x3c22010
PS: my custom cell is named "HangoverGameCell"
gabriellanata is offline   Reply With Quote
Old 01-26-2010, 01:28 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)theReuseIdentifier { 
	if (self = [super initWithStyle:style reuseIdentifier:theReuseIdentifier]) { 
		self = [[[NSBundle mainBundle] loadNibNamed:@"HangoverGameCell" owner:self options:nil] lastObject];
		self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gameCell.png"]];
		self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gameCellSelected.png"]];
	}
	return self; 
}
First you do the self = [super init...] thing. Then the very next line does self = somethingElse. Pretty sure that's a leak.

Also, both image views are leaked.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 01-26-2010, 01:56 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 3
Default

YES! thank you very much, that did it. I don't know how I didn't spot that


I also have one more question also about leaks. I'm experienced in programming mainly in PHP and Java but i'm new to this releasing thing. I have a problem that every time I do something like this:

Code:
questions = [[NSMutableArray alloc] init];
it leaks. I have a "questions" NSMutableArray which is defined in the header file and it says that this code leaks. I understand I have to do a [questions release]; somewhere but I don't know where (I already added it to the dealloc function). Btw, I already tried autorelease and it crashed the program. The questions array is used many times throughout the code, and i've made sure there is no other "init" or "alloc" to it.

Thanks again
gabriellanata is offline   Reply With Quote
Old 01-26-2010, 02:03 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 3
Default

woops, sorry found what was causing it. Still, thanks anyway, i really appreciate your help
gabriellanata is offline   Reply With Quote
Old 01-26-2010, 02:19 AM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

See the properties link in my signature.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
reloaddata, uitableview, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 253
19 members and 234 guests
ADY, Alsahir, Dani77, Desert Diva, Duncan C, F_Bryant, Grinarn, HemiMG, Herbie, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, Robiwan, smethorst, spiderguy84, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,761
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:42 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0