EXC_BAD_ACCESS when using stringWithFormat?

While deploying my application, I got the error: "Thread 1: Program received signal:" EXC_BAD_ACCESS ".

My code is below:

-(NSDictionary *)syncWithList:(NSInteger)listID
{
    NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%@",@"xxxxxxxxxxx",listID];
// **Here I got the error message: "Thread 1:Program received signal: "EXC_BAD_ACCESS"**
    NSLog(@"url: %@",urlit);
    NSURL *freequestionurl = [NSURL URLWithString:urlit];
    ASIHTTPRequest *back = [ASIHTTPRequest requestWithURL:freequestionurl];
    [back startSynchronous];
    self.listData = [[back responseString] objectFromJSONString];
    NSLog(@"%@",listData);
    NSDictionary *dicPost = [listData objectAtIndex:0];
    return dicPost;
}

      

Thank you so much!!!!

+3


source to share


4 answers


You don't have to format NSInteger

(which is only typedef'd int

for current iOS versions) with a specifier %@

. Notation %@

in string format basically means "call description

to object and use result".
But NSInteger is not an object, it is a primitive type.
You get a memory exception because when listID is 42, you are accessing the object at memory address 42. This is definitely not what you want.

-(NSDictionary *)syncWithList:(NSInteger)listID
                               ^^^^^^^^^
NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%@",@"xxxxxxxxxxx",listID];
                                                                                                     ^^

      



just use the format specifier %i

instead %@

for listID.

NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%i",@"xxxxxxxxxxx",listID];

      

+9


source


EDIT: so used to get errors from Xcode without giving me any clues. I forgot to notice that the problem line was already known. I will leave this here in the hopes that it helps someone in the future.

Try to create an exception breakpoint, it can point right to the line where your code crashes, which should help you figure out the problem.



  • Go to the breakpoints tab in the left navigator.
  • Click the little "+" button at the bottom.
  • Create a breakpoint as shown in the figure:
  • Run your code and see where it appears.

Breakpoint adding

+1


source


You made a very popular mistake on this line

NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%@",@"xxxxxxxxxxx",listID];

      

The second argument is of type NSInteger, but in the format you are using %@

, it is only an object, and the compiler assumes that your listID is the address of the object. The correct format is %li

:

NSString *urlit = [NSString stringWithFormat:@"http://0.0.0.0:3000/lists/%@/syncList.json?auth_token=%li",@"xxxxxxxxxxx",listID];

      

0


source


You have used the wrong type of data for printing. In the meantime, there is no need to know about it. โ€

NSLog(@"%@",listData);

      

0


source







All Articles