Can't locate EXC_BAD_ACCESS error - iphone

Or at least I can't figure out how to locate the error. I've read a ton of stuff but every time I go into my Product scheme and turn on Guard Malloc my program won't run.
The line of code that is giving me the problem is this... It happens on the NSString *str = ... line
- (void) setVolumeCompletionDate: (NSString *)volumeID: (double)completionDate
{
sqlite3_stmt *pStmt;
NSString *str = [NSString stringWithFormat:#"UPDATE Volumes SET GoalDate = %d WHERE VolumeID = '%#'", completionDate, volumeID];
//more stuff
}
If I hardcode the completionDate value (1350129600) on that line, the code works just fine, so it seems from everything I've read that the memory of completionDate is getting stepped on somewhere, or something bad is happening. My project is set up for ARC.
I think part of my problem is I don't even know where to go to begin to resolve this problem. I don't even know what to search for. All of the posts I've read about tracking it down with some Malloc tool don't make sense because I can't get that to run.
I would like to be able to figure this out. Maybe I need more, and complete steps, for debugging and tracing through things. Many of the answers I've read seem like they are written for people who know everything, because so much is left out of the answer, or so much is assumed about the person asking the question. If there is a better place for me to go to ask my question, please point me in that direction.
Thanks a lot. I appreciate your help.

completionDate is a double, so use %f instead of %d.
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Related

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.

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.

1 little error in my transition coding

http://img89.imageshack.us/img89/2554/screenshot20090910at154.png http://img89.imageshack.us/img89/2554/screenshot20090910at154.png
Full Size Image
Here you can see the code for the transition but it says there is an error before the "for"
What is it that i'm missing?
I basically want to do a fade in between images.
EDIT
Here is the updated code with the adjustments. I am still getting that error.
http://img186.imageshack.us/img186/7978/screenshot20090910at102.png http://img186.imageshack.us/img186/7978/screenshot20090910at102.png
Updated Code
The *NSArray** theImages line ends with the following
[UIImage imageNamed:#"image10.jpg"], [UIImage imageNamed:#"image11.jpg"], nil]];
The line before the for should read something like:
NSArray* theImages = [NSArray arrayWithObjects:[...], // object 1
..., // more objects
[...], // object N
nil]]; // nil must be last
Updated:
The error you are getting now is not the same as the one before. You'll need to look at more of your code in order to figure out what the error is referring to.
Also, can you post some of the code directly in your question instead of posting a picture? The font SO uses is easier to read than the one in the screenshots.
I don't know what language it is, but I beleave this line should end with ]; instead of ,
Your issues seem to be simple syntactical ones.
There is one caused by these two lines (can't see above it to see where the issue is
}
{
Also, you have a new issue with the NSArray* Definition because you have
NSArray* theImages]; = .....
^^This will cause an issue after you take care of the first one

iPhone: Reading Text From File and UISegmentedControl

First off, I'm a complete beginner.
That said, I thought an ambitious longer-term project/learning experience would be to create an app that displayed daily quotes, like those cheesy day-by-day calendars our grandmothers have in their bathrooms. I want it to have two per day, each one represented by a tab in a UISegmentedControl. That's the long term. Right now I'd be happy with getting a single day's worth of quotes functioning.
Onto the questions:
How can I get text saved in a .txt or .rtf file to be displayed in a UITextView? Preferably without using 'stringWithContentsOfFile,' since Xcode is telling me that's deprecated.
How can I get content from a different file (or maybe a different portion of the same file...?) to be displayed when the user taps the second segment?
If I can get it running so that those two conditions are met and I understand what's going on, I'll consider the day a success. Thanks!
1.
NSError *error = nil;
NSStringEncoding stringEncoding;
NSString *fileText = [NSString stringWithContentsOfFile:#"/path" usedEncoding:&stringEncoding error:&error];
myTextView.text = fileText;
The error and encoding are optional, and you can pass in nil for both. But if you care about the error, or what encoding the file was in they will have useful info in them after the string is created.
2.
Set the valueChanged outlet in Interface Builder to an IBAction on your controller, such as setSegmentValue:. Then, assuming you have an array of quote strings:
- (IBAction)setSegmentValue:(id)sender {
UISegmentedControl *control = (UISegmentedControl*)sender;
NSString *quote = [quotes objectAtIndex:control.selectedSegmentIndex];
myTextView.text = quote;
}
Even though stringWithContentsOfFile: is deprecated, stringWithContentsOfFile:usedEncoding:error: is not. That is the standard method to use for reading from files.
As for the second question, you simply test the state of the segmented control and perform as action based on it. Admittedly this is a high level answer but should get you going.

"Invalid" iphone string

I'm converting data (from a web page) to a string). The basic code works (but there's been some subtle change somewhere - maybe on server).
NSLog shows the expected string (maybe 1000 chars long). However, when I float over responseString, it shows "Invalid". Worse, parsing with componentsSeparatedByCharactersInSet does not work.
Ideas?
NSString *responseString;
responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog([NSString stringWithFormat:#"responsestring ='%#'",responseString]);
if ([responseString compare:#""] != NSOrderedSame) {
lines = [responseString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#";"]];
This may happen when the configuration is set to "Release" rather than "Debug" I think.
Do not trust what the debugger says, it is not accurate, this has happened to me and took me a while to realise that the xcode debugger is not always right and should not be trusted, so i no longer trust the debugger and i use nslog statements whenever it tries to tell me something is invalid. So dont worry about it it happens, and when it happened to me I was also parsing responses from some webservice.
Just to be clear -- my experience with seeing "Invalid" in the debugger means that the reference is to an already-released object.
Your question and the comments below seem to suggest that you are thinking "Invalid" is an actual string value -- but are you sure you don't just have a memory management probably?