Strange Error:-CoreAnimation: ignoring exception: - iphone

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:)

Related

Bugg with NSDictonary sometimes crashes with "Trying to insert nil" response

I´m having a great deal of trouble with a bugg that in some cases gives the "initWithObjects:forKeys:count:]: attempt to insert nil object from objects[6]'" error message. It´s only occurring around every third trial and in the other cases it works fine. I haven´t debugged buggs that happeds at certain situations so that´s whyI´m asking for help. It might be difficult to find a solution but I would be glad to receive some advice atleast where to start look for the issue and how to debug it?
Basically my app have a method that sends a NSDictonary list with objects to a server. In some cases I want to remove an object if the user have´t chosen anything for that list. Problem is that the server died´t like sending an object with an empty space. So I made a kind of not nice looking work around. Basically The IBaction checks if the Object contains nil. If it contains nil it redirects to a very similar NSDictonary without the Object containing Nil. It woks in some cases but it seems like it sometimes don´t recognize the object containing nil.
Here´s the code in Inaction that redirects the code. I haven´t created a method for inserting anything to the object:#"DefaultCreateImageID" yet so as far as I know it should always be nil.
NSString *Imagestuff= [[NSUserDefaults standardUserDefaults] objectForKey:#"DefaultcreateImageID"];
if ( [[NSUserDefaults standardUserDefaults] objectForKey:#"DefaultcreateImageID"] != ( NSString *) [NSNull null] ){
DataHandler *handler = [DataHandler sharedDatahandler];
handler.authenticateUserDelegate = self;
[handler createActivityNoImage];
NSLog(Imagestuff);
}
else
{
DataHandler *handler = [DataHandler sharedDatahandler];
handler.authenticateUserDelegate = self;
[handler createActivity];
}
Thanks!
If you get an error "Trying to insert nil" then one of the objects you are trying to add to the dictionary is nil. Solution: Don't do that.
Figure out why some item you are trying to add to the dictionary is nil. Then figure out what you want to do in the situation.
To follow up on gnashers729 answer. The problem for me isn't nil but the fact my Inaction fails to reqognize what the object contains.
Here for example I tried to replace nil with a string to reqognize. I get the same problem that my method in some cases recognize the string and in some cases don´t.
NSString *Imagestuff= [[NSUserDefaults standardUserDefaults] objectForKey:#"DefaultcreateImageID"];
if ( [[NSUserDefaults standardUserDefaults] objectForKey:#"DefaultcreateImageID"] != ( NSString *) #"noImage" ){

Preventing array index crash by checking array count, is there any performance issue ?

For avoiding crash issue, if I check the array count before accessing every index like the following code then is there any performance issue? Is it a good practice or bad practice for big apps ?
My Code is:
-(BOOL)checkIndexAvailability:(NSArray *)array_ withIndex:(int)index_
{
if ((array_.count-1)>=index_) {
return TRUE;
}
return FALSE;
}
When I access any array I call like this:
if ([self checkIndexAvailability:regDataArray withIndex:2]) {
lastName = [regDataArray objectAtIndex:2];
}
I would suggest just doing a quick inline check to make it a little bit better. Something like this:
lastName = (regDataArray.count - 1 > 2) ? [regDataArray objectAtIndex:2] : nil;
Although you wont notice any speed with difference with your way or mine.
With the help of Alladanian, brainLikeApple and Kris Gellic's answer, I understand that preventing the Array Index out of bounds crash issue can be solved by following this way:
int arrayCount = [regDataArray count];
NSString *lastName = ( 2 <arrayCount) ? [regDataArray objectAtIndex:2] : #"Last Name Not Found";
NSString *address = ( 4< arrayCount) ? [regDataArray objectAtIndex:4] : #"No Address Found";
There don't need to create another method or accessing array count method every time.
I think that doing that way should not delay your app that much.
But take in account that when your're programming, if things are done the right way your programm should never try to get something of an array position that does not exist.
You can find some more information Bounds checking
I think it can be encapsulated in category. Or maybe, you can change standard implementation using runtime library.
Also it will be great if you add something like assertion in debug mode, so you don't hide issues in implementation which send wrong indexes.
P.S. I don't think it can dramatically affect on performance.
I don't think there is any reason to add checks like this because NSArray already does similar checks. It raises an exception when the index is invalid.
Now, maybe you are asking why I think that raising an exception is better then silently skipping objectAtIndex. Well, if you are trying to index something that's not there, it's a bug. Silently skipping the code only hides the bug from you.

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/

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.