Reputation: 4166
I have ONE viewController that is giving me a problem...
UIViewController *nextController = [[NextView alloc] initWithNibName:@"NextView" bundle:nil];
[currentPageController.view removeFromSuperview];
[self.view addSubview:nextController.view];
My app crashes here with an EXC_BAD_ACCESS.
Does anybody have ANY idea what could cause this?
Thanks in advance!
UPDATE
After using Breakpoints and stepping through the code, the problem seems to be with this bit of code in the viewDidLoad of my viewController:
NSString *noteToSet;
if ([Settings isData]) {
noteToSet = [NSString stringWithFormat:@"Data, "];
}
if ([Settings isGeom]) {
if ([noteToSet isEqualToString:@""]) {
noteToSet = [NSString stringWithFormat:@"Geom, "];
} else {
noteToSet = [noteToSet stringByAppendingFormat:@"Geom, "];
}
}
Anybody see a problem there? Thanks so much!
FIXED
Fixed it by initializing the string with the blank value @""
noteToSet = [NSString stringWithFormat:@""];
Upvotes: 0
Views: 74
Reputation: 4166
So the first part of the answer is - if your viewController won't load and you have no idea why - check the code in viewDidLoad, that's where my issue was and it drove me crazy trying to figure out what was wrong with the viewController itself when it was really an NSString issue in the viewDidLoad all along.
The second part is that you can't compare an NSString to a blank value using [stringName isEqualToString:@""] unless you got that string from NSUSerDefaults or unless you first set the string to be equal to @"".
Upvotes: 1
Reputation: 4399
EXC_BAD_ACCESS is often caused by poor memory management. Go to the Build Menu in Xcode and Profile it (in the simulator) using Allocations. Then go in and make sure you have Zombies Enabled. Run the app in the simulator and point it to where you get the error. Instruments should then tell you where the bad memory management is. If you still can't get it, then tell us what you're getting.
Here's a guide: http://www.markj.net/iphone-memory-debug-nszombie/
Upvotes: 0
Reputation: 125037
I don't see anything in the posted code that'd cause the exception. However, both pieces of code that you posted contain the lines:
currentPageController = nextController;
[currentPageController retain];
[nextController release];
Since the first line makes currentPageController point to the same object as nextController, the second and third lines cancel each other out. You might as well write:
currentPageController = nextController;
and leave it at that. A similar misunderstanding at some other point in the code could easily cause you to miss a retain or release once too often and cause the sort of bad pointer that you seem to be seeing.
Upvotes: 0