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 03-18-2010, 10:34 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 2
Default "EXC_BAD_ACCESS" after viewWillAppear

I'm working on an App, where the User can click on an item in a tableView and gets forwarded to a new Controller, which I call ProductController. This Controller loads Image and Description from Remote given the specific productId. Nothing special so far.

It pushes the ProductController, calls viewDidLoad and viewWillAppear successfully. But it doesnt show the View, instead it goes back to didSelectRowAtIndexPath in the former Controller and then crashes. In Debug-Mode the console gives me at least following line:
Code:
Program received signal:  “EXC_BAD_ACCESS”.
Basically this is the Code inside the method didSelectRowAtIndexPath in my TableView:
Code:
	NSString* id = [[[entries objectAtIndex: storyIndex]productId]retain];
	
	ProductController *productController = [[ProductController alloc] init:id];
	[[self navigationController]pushViewController:productController animated:YES];
	[productController release];
This is the Code in my Init Method of my ProductController:
Code:
self = [super init];   

	if (managedObjectContext == nil) 
	{ 
        managedObjectContext = [(myRSSAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; managedObjectContext);
	}
	
	if (self) {
		product = (Product *)[NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:managedObjectContext];
		[product setValue: productId forKey:@"id"];

		NSSet *priceSet = [[NSSet alloc]init];
		[product setValue: priceSet forKey:@"prices"];

	}
This is the Code in viewWillAppear in my ProductController:
Code:
[super viewWillAppear:animated];
And this is the Code in viewDidLoad in my ProductController:
Code:
- (void)viewDidLoad {
	NSLog(@"begin Productcontroller viewDidLoad");
	[super viewDidLoad];
//	BackButton
	self.title = @"Produktinfos";
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"zurück" style: UIBarButtonItemStylePlain target:nil action:nil];
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];
	//Save Button
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
	initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save_Clicked:)]autorelease];

	NSMutableString * path = [NSMutableString stringWithCapacity:100];
	[path appendString:@"<myURL>"];
	[path appendString:[product valueForKey:@"id"]];
	[self parseXMLFileAtURL:path];
	[path release];

	//Product
	[titleLabel setText: [product valueForKey:@"name"]];
	[descriptionLabel setText:[product valueForKey:@"productdescription"]];
	[descriptionText setText:[ product valueForKey:@"productdescription"]];
	
	//price format
	
	formatter = [[NSNumberFormatter alloc] init];
	[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_EN"] autorelease]];
	[formatter setMinimumFractionDigits:2];
	[formatter setGeneratesDecimalNumbers:TRUE];
	
	NSNumber *number = [formatter numberFromString:[product valueForKey:@"topprice"]];
	float result = [number floatValue];
	[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"] autorelease]];
	
	[priceLabel setText: [formatter stringFromNumber:[NSNumber numberWithFloat:result]]];
	[formatter release];
	
	//picture
	NSMutableString *urlString = [NSMutableString stringWithCapacity: 100];
	[urlString appendString: @"<myURL>;
	[urlString appendString: [product valueForKey:@"id"]];
	[urlString appendString: @".jpg"];
	
	NSURL * url = [NSURL URLWithString:urlString];
	
	image = [UIImage imageWithData: [NSData dataWithContentsOfURL: url]];
	self.imageView.image = image;

	//CGSize size = CGSizeMake(10,1000);
	
	[scrollView setContentSize:dataView.bounds.size];

	//[scrollView setContentSize:size];	 
	// + (id)sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending
	//NSSortDescriptor * descriptor = [NSSortDescriptor initWithKey: key ascending:ascending];
	NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"retailerid" ascending:YES];
	NSArray * descriptorArray = [NSArray arrayWithObject: descriptor];
	
	NSSet * set = [product valueForKey:@"prices"];
	NSMutableArray* array = [NSMutableArray arrayWithCapacity:[set count]];
	for (id anObject in set)
		[array addObject:anObject];
	[array sortUsingDescriptors: descriptorArray];
	self.priceList = array;
	NSLog(@"end ProductController viewDidLoad");
	return;
	
}
I'm working with CoreData the first time and I'm not sure if I'm doing something wrong here. Googling “EXC_BAD_ACCESS” it seems that there is something wrong with my memorymanagement but i just can't see where. Would be nice, if someone could help me.

Last edited by paschi; 03-18-2010 at 10:36 AM.
paschi is offline   Reply With Quote
Old 03-18-2010, 10:46 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Code:
	NSString* id = [[[entries objectAtIndex: storyIndex]productId]retain];
	
	ProductController *productController = [[ProductController alloc] init:id];
	[[self navigationController]pushViewController:productController animated:YES];
	[productController release];
1. id is leaked (the retain is not needed)
2. I'd suggest avoiding using "id" as a variable name.

Code:
self = [super init];   

	if (managedObjectContext == nil) 
	{ 
        managedObjectContext = [(myRSSAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; managedObjectContext);
	}
	
	if (self) {
		product = (Product *)[NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:managedObjectContext];
		[product setValue: productId forKey:@"id"];

		NSSet *priceSet = [[NSSet alloc]init];
		[product setValue: priceSet forKey:@"prices"];

	}
1. priceSet is leaked.
2. Not much point in creating an empty NSSet. Perhaps you mean NSMutableSet.

Code:
- (void)viewDidLoad {
	NSLog(@"begin Productcontroller viewDidLoad");
	[super viewDidLoad];
//	BackButton
	self.title = @"Produktinfos";
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"zurück" style: UIBarButtonItemStylePlain target:nil action:nil];
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];
	//Save Button
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
	initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save_Clicked:)]autorelease];

	NSMutableString * path = [NSMutableString stringWithCapacity:100];
	[path appendString:@"<myURL>"];
	[path appendString:[product valueForKey:@"id"]];
	[self parseXMLFileAtURL:path];
	[path release];

	//Product
	[titleLabel setText: [product valueForKey:@"name"]];
	[descriptionLabel setText:[product valueForKey:@"productdescription"]];
	[descriptionText setText:[ product valueForKey:@"productdescription"]];
	
	//price format
	
	formatter = [[NSNumberFormatter alloc] init];
	[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_EN"] autorelease]];
	[formatter setMinimumFractionDigits:2];
	[formatter setGeneratesDecimalNumbers:TRUE];
	
	NSNumber *number = [formatter numberFromString:[product valueForKey:@"topprice"]];
	float result = [number floatValue];
	[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"] autorelease]];
	
	[priceLabel setText: [formatter stringFromNumber:[NSNumber numberWithFloat:result]]];
	[formatter release];
	
	//picture
	NSMutableString *urlString = [NSMutableString stringWithCapacity: 100];
	[urlString appendString: @"<myURL>;
	[urlString appendString: [product valueForKey:@"id"]];
	[urlString appendString: @".jpg"];
	
	NSURL * url = [NSURL URLWithString:urlString];
	
	image = [UIImage imageWithData: [NSData dataWithContentsOfURL: url]];
	self.imageView.image = image;

	//CGSize size = CGSizeMake(10,1000);
	
	[scrollView setContentSize:dataView.bounds.size];

	//[scrollView setContentSize:size];	 
	// + (id)sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending
	//NSSortDescriptor * descriptor = [NSSortDescriptor initWithKey: key ascending:ascending];
	NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"retailerid" ascending:YES];
	NSArray * descriptorArray = [NSArray arrayWithObject: descriptor];
	
	NSSet * set = [product valueForKey:@"prices"];
	NSMutableArray* array = [NSMutableArray arrayWithCapacity:[set count]];
	for (id anObject in set)
		[array addObject:anObject];
	[array sortUsingDescriptors: descriptorArray];
	self.priceList = array;
	NSLog(@"end ProductController viewDidLoad");
	return;
	
}
1. Path is over-released.

Neither of your mutable strings need to be mutable strings. Look into the formatting options for plain strings.
__________________
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 03-18-2010, 10:47 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 2
Default

I'm sorry for posting, but I think I found the error. For anyone who is interested:
I released the NSMutableString path, which i didnt allocate or retained myself.
paschi is offline   Reply With Quote
Old 03-27-2010, 02:55 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 12
Default

Quote:
Originally Posted by paschi View Post
I'm sorry for posting, but I think I found the error. For anyone who is interested:
I released the NSMutableString path, which i didnt allocate or retained myself.
Cheers! I had the same problem.
D7BUSTxUYtx1HZ is offline   Reply With Quote
Reply

Bookmarks

Tags
core data, exc_bad_access, leak, memory management

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: 266
17 members and 249 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, Duncan C, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
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:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0