Alexey
Alexey

Reputation: 11

NSString EXC_BAD_ACCESS objective C

I'm new in Objective C and I have some problem.... It's my code: 1)

testAppDelegate.h (not all):
    @interface testAppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet NSWindow *windowLogin;
    IBOutlet NSWindow *windowContactList;
    IBOutlet NSTextField *memStatus;
    NSString *access_token, *expires_in, *user_id; 
    NSMutableArray *records;}

2) testAppDelegate.m (not all):

    int posInStr(NSString *subString, NSString *genString){
    NSRange match;
    match = [genString rangeOfString:subString];
    return match.location;
}

NSString* pars(NSString *str_,NSString *str,NSString *_str){
    NSString *tmp;
    int startPos = posInStr(str_,str) + [str_ length];
    tmp = [str substringFromIndex:startPos];
    int finishPos = posInStr(_str, tmp);
    return [tmp substringToIndex:finishPos];
}

-(IBAction)but2Click: (id)sender{
    NSString *tmp2 = access_token;
    NSString *tmp = [NSString stringWithFormat:@"https://api.vkontakte.ru/method/messages.getDialogs?count=3&access_token=%@",tmp2];
    NSURL *url = [NSURL URLWithString:tmp];
    NSLog(@"%@",tmp);
    NSLog(@"%@",url);
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(showLoaded)];
        [request startAsynchronous];
}

-(IBAction)but1Click:(id) sender{
    NSURL *url = [NSURL URLWithString:@"http://api.vkontakte.ru/oauth/authorize?client_id=293&scope=friends,messages&redirect_uri=http://api.vkontakte.ru/blank.html&display=popup&response_type=token"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinishedtest:)];
    [request startAsynchronous];
}
- (void)requestFinishedtest:(ASIHTTPRequest *)request
{   
    [memStatus setStringValue:@"Loading..."];
    NSString *tmp = [NSString stringWithFormat:@"%@",[request url]];
    [tmp retain];
    access_token = pars(@"access_token=", tmp, @"&");
    NSLog(@"%@",access_token);
    expires_in = pars(@"expires_in=", tmp ,@"&");
    user_id = pars(@"user_id=", [NSString stringWithFormat:@"%@&",tmp], @"&");
    [memStatus setStringValue:@"Logined"];
    [windowLogin orderOut:nil];
    [windowContactList makeKeyAndOrderFront:self];
    [NSApp activateIgnoringOtherApps:YES];
}

My problem: "EXC_BAD_ACCESS" in "but2Click"

Upvotes: 1

Views: 870

Answers (2)

Lou Franco
Lou Franco

Reputation: 89172

It's going to be really hard to figure this out from code -- you are going to have to debug it.

I wrote this blog to help understand and debug EXC_BAD_ACCESS

Basically, you are dereferencing a pointer that is pointing to memory that isn't allocated to your process. The main reasons that this could happen are

  1. You are using an object that has been deallocated
  2. The heap is corrupt

The things you should do to debug this:

  1. Do a Build and Analyze. The reports of leaks are bad, but not related to this issue -- you want to look for issues of too few retains

  2. Turn on Zombies and run in the debugger. Now, none of your objects will be deallocated, but when they have a retain count 0, they will complain to the debugger if you use them.

There are other tips on the blog that are a little harder to explain

Upvotes: 1

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You are assigning an autoreleased object here:

access_token = pars(@"access_token=", tmp, @"&");

access_token must be getting released before the but2click method is invoked by a button tap.

You need to retain it if you want to use it later.

Upvotes: 1

Related Questions