NSString EXC_BAD_ACCESS 目标 C

发布于 2024-11-11 00:28:53 字数 2446 浏览 4 评论 0原文

我是 Objective C 的新手,我有一些问题...... 这是我的代码: 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(不是全部):

    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];
}

我的问题: “but2Click”中的“EXC_BAD_ACCESS”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮云落日 2024-11-18 00:28:53

您在此处分配一个 autoreleased 对象:

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

access_token 必须在通过按钮点击调用 but2click 方法之前释放。

如果您以后想使用它,则需要保留它。

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.

神妖 2024-11-18 00:28:53

从代码中弄清楚这一点非常困难——您必须对其进行调试。

我写了这个 博客来帮助理解和调试 EXC_BAD_ACCESS

基本上,您正在取消引用指针它指向未分配给您的进程的内存。发生这种情况的主要原因是

  1. 您正在使用已被释放的对象
  2. 堆已损坏

您应该执行以下操作来调试此问题:

  1. 执行构建和分析。泄漏报告很糟糕,但与此问题无关 - 您想要查找保留太少的问题

  2. 打开僵尸并在调试器中运行。现在,您的任何对象都不会被释放,但是当它们的保留计数为 0 时,如果您使用它们,它们会向调试器抱怨。

博客上还有其他一些有点难以解释的提示

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文