NSString EXC_BAD_ACCESS 目标 C
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在此处分配一个
autorelease
d 对象:access_token
必须在通过按钮点击调用but2click
方法之前释放。如果您以后想使用它,则需要保留它。
You are assigning an
autorelease
d object here:access_token
must be getting released before thebut2click
method is invoked by a button tap.You need to retain it if you want to use it later.
从代码中弄清楚这一点非常困难——您必须对其进行调试。
我写了这个 博客来帮助理解和调试 EXC_BAD_ACCESS
基本上,您正在取消引用指针它指向未分配给您的进程的内存。发生这种情况的主要原因是
您应该执行以下操作来调试此问题:
执行构建和分析。泄漏报告很糟糕,但与此问题无关 - 您想要查找保留太少的问题
打开僵尸并在调试器中运行。现在,您的任何对象都不会被释放,但是当它们的保留计数为 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
The things you should do to debug this:
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
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