Help using NSZombieEnabled - iphone

I'm trying to debug some code. I ran the static analyzer, and thought I fixed a memory leak, and now I get an error when I switch between two tabs. Here is my code when I switch between the 2nd tab and first tab:
if (_sortButton != nil) {
self.SortButton = nil;
NSMutableArray *barItems = [[self.MainToolbar items] mutableCopy];
[barItems removeObjectAtIndex:0];
[self.MainToolbar setItems:barItems]; // bad access here
[barItems release];
}
I keep getting the EXC_BAD_ACCESS on the self.MainToolbar setItems line. I added the NSZombieEnabled as an environment variable, set a break point at that bad access line, but I do not get anything printed to the console when either stepping after the breakpoint, hitting continue after the breakpoint, etc. Am I using this correctly? Thanks.

Far easier to use than NSZombieEnabled, is to use Profile instead when running and select the NSZombie instrument.
This requires XCode4.

Please try adding a ,nil at the end of a list of objects for the NSArray.
[self.MainToolbar setItems:barItems,nil]

Related

Strange Error:-CoreAnimation: ignoring exception:

I am developing an iPhone application and everything seems to work fine.
But when I use the app for some amount of time suddenly on view load
CoreAnimation: ignoring exception: *** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]
comes and after that the view life cycle methods are not called (basically the app stop responding).
Please suggest me if anyone knows the significance of this error or how do I resolve it.
Did you check if you get any memory issues just before this error message pops up? Implement the didReceiveMemoryWarning method in all your viewcontrollers (put in NSLog statements for now) and see if any of these are printed before you get this error.
Finally I got solution to my problem , it really very strange but would like to share with all of you so that if you guys face the same error, can check for this:-
On debugging my whole project I came to know on a when I click on a certain button and then click on any where else so that that page viewWillDisappear Method gets called.
-(void)viewDidDisappear:(BOOL)animated
{
NSString *finalTimeString=self.timeLabel.text;
if (![finalTimeString isEqualToString:#""] || finalTimeString!=nil) {
NSArray *aRR =[finalTimeString componentsSeparatedByString:#"-"];
NSString *startTimeString = [[aRR objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *endTimeString = [[aRR objectAtIndex:1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[appDelegate.searchDataDictionary setObject:startTimeString forKey:#"time_str"];
[appDelegate.searchDataDictionary setObject:endTimeString forKey:#"time_end"];
}
}
I came to know that when I got finalTimeString empty then that error came and if finalTimeString contain some data then it works fine.So I put condition according to my self and got rid from this error.
Happy Coding:)

bad UIViewController

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:#""];
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 #"".
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.
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/

Why is my application freezing when changing views?

I have this same code at the end of each view to go to the next:
- (IBAction)proceed2 {
StepThree *one = [[[StepThree alloc] initWithNibName:#"StepThree" bundle:nil] autorelease];
one.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:one animated:YES];
}
This has worked in every view except this one, and all of my code looks exactly the same except for different step numbers (not always StepThree). I imported "StepThree.h" just as I did in all of them... why is this one giving me problems?
By the way, it is in the line
[self presentModalViewController:one animated:YES];
and the error is "Thread 1: Program received signal: "SIGABRT"."
The code you post seems correct.
The problems arises presumably due to something in StepThree implementation.
A good way to diagnose this kind of bad behavior is enabling "zombies" detection. See here for details.

Trashed properties in Application Delegate

I'm in deep trouble. Something in my app causes a lot of properties in my app delegate to become trashed (changing contents and even object type, say an NSArray becomes an NSString...), and I can't debug it out. I can find no memory leaks or errors on my part. The only thing I've found is that all the way to ViewDidAppear for the view of the first tab, everything is okay. The view displays a table. When one of the cells are clicked, the app delegate properties are already trashed.
What after a view has been loaded and before didSelectCellForRow could cause this? No other code of mine is being executed between those two, certainly no code in the app delegate.
Any tips for hunting this down in an sleuthy manner would be appreciated, or just some thoughts on narrowing it down to what could cause it.
It sounds like either something is getting released prematurely or things are not properly connected with respect to one of your XIBs. If you haven't already, you might want to familiarize yourself with NSZombieEnabled, NSDeallocateZombies, NSEnableAutoreleasePool and NSAutoreleaseFreedObjectCheckEnabled. These are environment variables that can be set in the Executable "Get Info" window's Arguments panel.
For sanity's sake, I have added this to my AppDelegate's -applicationDidFinishLaunching:
#ifdef DEBUG
// account for environment value's actual value if set.
NSString *NSZombieEnabled = (getenv("NSZombieEnabled"))
? [NSString stringWithCString:getenv("NSZombieEnabled") encoding:NSASCIIStringEncoding]
: #"NO";
DLog(#"NSZombieEnabled = %#", NSZombieEnabled );
NSString *NSDeallocateZombies = (getenv("NSDeallocateZombies"))
? [NSString stringWithCString:getenv("NSDeallocateZombies") encoding:NSASCIIStringEncoding]
: #"NO";
DLog(#"NSDeallocateZombies = %#", NSDeallocateZombies );
NSString *NSEnableAutoreleasePool = (getenv("NSEnableAutoreleasePool"))
? [NSString stringWithCString:getenv("NSEnableAutoreleasePool") encoding:NSASCIIStringEncoding]
: #"YES";
DLog(#"NSEnableAutoreleasePool = %#", NSEnableAutoreleasePool );
NSString *NSAutoreleaseFreedObjectCheckEnabled = (getenv("NSAutoreleaseFreedObjectCheckEnabled"))
? [NSString stringWithCString:getenv("NSAutoreleaseFreedObjectCheckEnabled") encoding:NSASCIIStringEncoding]
: #"NO";
DLog(#"NSAutoreleaseFreedObjectCheckEnabled = %#", NSAutoreleaseFreedObjectCheckEnabled );
#endif
It sometimes saves me from having to check these variables through Xcode UI.
The only way out was to wade through every alloc in the app delegate and a few viewcontrollers and also made sure I knew what'd happen using the NSCopying protocol. There were 2 errors due to synthesized but nil (and then reassigned!) objects, and 1 copy-error, one or more of them caused the trashing when there was an early "autorelease" by Objective-C.

Issue posting variable with iPhone SDK

I am encountering an issue while posting a variable with Xcode:
While running this code the app crashes while posting the variable to a webservice:
NSArray *array = [stringFromFile componentsSeparatedByString: #","];
NSString *time = [array objectAtIndex:1];
UTCorLocal = time;
NSLog(#"%#", UTCorLocal);
UTCorLocal variable is declared earlier in the code. The NSLog outputs the correct string, but when I try to use it further along in the code it crashes.
When I give the variable a static value like this:
UTCorLocal = #"UTC";
It all runs like it should do!
Could anybody please help, it's driving me crazy!
Thanks a lot,
Ron
It's probably released somewhere along the way, I would guess, without knowing the rest of your code. Try copying or retaining 'time' and see what happens.