iPhone EXC_BAD_ACCESS 访问块中的实例变量
我有一个关于实例变量与块和块相结合的问题IOS5 中 Objective C 的 arc。
不久,当我访问此代码时,iPhone 给我一个 EXC_BAD_ACCESS 并终止:
- (void) doRequest: (void (^)(XMLTreeNode*) )completionHandler {
NSString * urlString = [NSString stringWithFormat:@"blablaurl=%@&", action];
for( NSString* key in parameters ){
urlString = [urlString stringByAppendingFormat:@"&%@=%@", key, [parameters objectForKey:key]];
}
NSURL * url = [NSURL URLWithString:urlString];
NSLog( @"Visiting: %@", [url absoluteString] );
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * response, NSData * data, NSError * err) {
NSLog( @"Params=%@", parameters );
completionHandler(e);
}];
}
exc_bad_access 发生在: NSLog( @"Params=%@", 参数 );
(参数是类的实例变量)..只是在头文件中定义,没有特殊属性或什么的..
为什么它会崩溃以及如何防止它?谢谢!
我的猜测是,它崩溃是因为对象的生命周期在 doRequest 调用之后结束,因此 ARC 清理了所有变量(以及参数 var)。当 urlconnection 完成并调用块时,实例变量已准备好清理..
I've got a question about instance variables in combination with blocks & arc in Objective C with IOS5.
Shortly, when i access this code, the iPhone gives me an EXC_BAD_ACCESS and terminates:
- (void) doRequest: (void (^)(XMLTreeNode*) )completionHandler {
NSString * urlString = [NSString stringWithFormat:@"blablaurl=%@&", action];
for( NSString* key in parameters ){
urlString = [urlString stringByAppendingFormat:@"&%@=%@", key, [parameters objectForKey:key]];
}
NSURL * url = [NSURL URLWithString:urlString];
NSLog( @"Visiting: %@", [url absoluteString] );
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * response, NSData * data, NSError * err) {
NSLog( @"Params=%@", parameters );
completionHandler(e);
}];
}
The exc_bad_access occurs on:
NSLog( @"Params=%@", parameters );
(parameters is an instance variable of the class).. Just defined in the header file, no special property or what-so-ever..
Why does it crash and how can i prevent it? Thanks!
My guess is that it crashes because the objects lifetime is over after the doRequest call, and thus ARC cleans up all variables (and with that the parameter var).. When the urlconnection completes and calls the block, the instance variables are aready cleaned up..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
参数由 ARC 清理。
这里有 2 种情况:
您的主对象在块完成之前没有释放:只需为“参数”创建一个强的非原子属性。在你的属性中使用“strong”关键字告诉 ARC,你在整个主对象生命周期中都需要“参数”
你的主对象在块完成之前被释放:创建一个指向你的对象的新 __block 指针
使用“__block”关键字说ARC 表明您在整个区块生命周期中都需要“区块参数”
parameters is clean up by ARC.
2 case here:
Your main object isn't released before the block completion: Just create an strong,nonatomic property for "parameters". Using the "strong" keyword in your property say to ARC that you need "parameters" during all your main object life
Your main object is released before the block completion: create a new __block pointer to your object
Using the "__block" keyword say to ARC that you need "blockParameters" during all your block life
您只能使用该块的参数,即本例中的响应、数据和错误。您可以使用
[response URL]
来获取参数。You have only the parameters of the block at your disposal, i.e.
response
,data
anderror
in this case. You could use[response URL]
to get at the parameters.