简体   繁体   English

随机BAD_ACCESS和“无法识别的选择器”

[英]Random BAD_ACCESS and “not recognized selector”

Hey, I'm a beginner in Objective C, and my .NET and Java years have rusted my memory management skills, so it's very likely I'm missing something here. 嘿,我是Objective C的初学者,而.NET和Java的岁月使我的内存管理技能生锈了,因此很可能在这里遗漏了一些东西。

I am building an iPad app. 我正在构建一个iPad应用程序。 The main view is a SplitView with a TableView on the left, and the detail view contains another TableView. 主视图是一个SplitView,左侧是TableView,详细视图包含另一个TableView。 The loading of the latter with data has been commented out in an attempt to single out my problem. 后者的数据加载已被注释掉,以试图指出我的问题。

The app seems to work fine (has to fetch data from a .NET WS and parse it into the table), but at random times I receive a BAD_ACCESS or a "selector not recognized" errors. 该应用程序似乎运行良好(必须从.NET WS提取数据并将其解析到表中),但是在随机时间,我会收到BAD_ACCESS或“选择器无法识别”错误。

The selector not recognized error I get here: 选择器无法识别我在这里得到的错误:

-(void) connection:(NSURLConnection *) connection
didReceiveResponse:(NSURLResponse *) response {
    [webData setLength: 0];
}

This piece of code I'm sure most of you know about, I got it from all samples I found online and in books to send a web request. 我敢肯定,大多数人都知道这段代码,我从网上和书中找到的所有示例中获取了这些代码,以发送Web请求。

Beats me why it says it does not recognize the setLength selector, webData is defined as 困扰我,为什么它说它不能识别setLength选择器,所以webData被定义为

NSMutableData *webData;

Any ideas? 有任何想法吗?

Thanks. 谢谢。

Most likely you aren't creating the NSMutableData correctly. 很可能您没有正确创建NSMutableData。 I expect you have code that looks like 我希望你有看起来像的代码

webData = [NSMutableData data];

This is going to give you an autoreleased object, and yet you're storing it in an ivar. 这将为您提供一个自动释放的对象,但是您将其存储在一个ivar中。 You need to take ownership of the object when storing it in an ivar. 将对象存储在ivar中时,需要获得对象的所有权。 In your case, the simplest way is just to skip the convenience method and go with alloc/init: 在您的情况下,最简单的方法就是跳过便捷方法并使用alloc / init:

webData = [[NSMutableData alloc] init];

For more details, read the Memory Management Programming Guide . 有关更多详细信息,请阅读《 内存管理编程指南》

Seems to be very usual (not only beginners) error, when connection is not cancels in dealloc or viewWillDisappear . deallocviewWillDisappear未取消连接时,这似乎是非常常见的错误(不仅是初学者)。 When you are leaving the controller, you should cancel all connection, timers, etc, created by the controller, to prevent them from calling delegate methods or selectors on deallocated controller objects. 离开控制器时,应取消控制器创建的所有连接,计时器等,以防止它们调用已释放控制器对象上的委托方法或选择器。

If you do not allocate your webData object either with 如果您不分配您的webData对象

NSMutableData* webData = [[NSMutableData alloc] initWithCapacity:2048];

or 要么

NSMutableData* webData = [[NSMutableData data] retain];

then the webData object will most likely be autoreleased during one of the context switches from the NSURLConnection message that you allocated it in (probably connection:didReceiveData: ) to the connection:didReceiveResponse: message. 那么该webData对象很可能会在上下文切换之一中从您在其中分配了NSURLConnection消息(可能是connection:didReceiveData: didReceiveData:)到connection:didReceiveResponse:消息中自动释放。

Any object that you do not alloc or explicitly retain is likely to be deallocated during scope changes, even if it is a member variable of your class. 你不要任何对象alloc或明确retain很可能在范围变更被释放,哪怕是你的类的成员变量。

It looks like webData is being deallocated and replaced with some other object. 似乎webData已被释放,并被其他对象替代。 Make sure you retain it if you don't use alloc/init or mutableCopy to get it. 如果不使用alloc / init或mutableCopy来获取它,请确保保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM