Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 我是否需要将release发送到dealloc方法中的实例变量?(iOS)_Iphone_Objective C_Ios_Memory Management - Fatal编程技术网

Iphone 我是否需要将release发送到dealloc方法中的实例变量?(iOS)

Iphone 我是否需要将release发送到dealloc方法中的实例变量?(iOS),iphone,objective-c,ios,memory-management,Iphone,Objective C,Ios,Memory Management,在我的类中,我有 - (void) dealloc { [searchField release]; [super dealloc]; } 其中searchField在类变量中定义 @interface SearchCell : UITableViewCell { UISearchBar *searchField; id delegate; } 该类按以下方式使用: if (indexPath.section == 0) { Sear

在我的类中,我有

- (void) dealloc
{
    [searchField release]; 
    [super dealloc];
}
其中searchField在类变量中定义

@interface SearchCell : UITableViewCell
{
    UISearchBar *searchField;
    id delegate;
}
该类按以下方式使用:

if (indexPath.section == 0)
    {
        SearchCell *mycell = [[SearchCell alloc] init];
        [cell setDelegate:self];
        return [mycell autorelease];
    }
searchField在此处创建:

- (id) init
{
    self = [super initWithFrame:CGRectZero];

    [self create];

    return self;
}

- (void) create
{
    searchField = [[UISearchBar alloc] initWithFrame:CGRectZero];
    searchField.autocorrectionType = UITextAutocorrectionTypeNo;    
    [self addSubview:searchField];
}

我是否需要使用[searchField release];在我的房间里?应用程序崩溃,并显示以下消息:“*[UISearchBar respondsToSelector:]:message sent to deallocated instance*”

否,不要在此处释放它。由于使用“autorelease”返回它,下一次运行事件循环将自动减少保留计数


当您最终点击“dealloc”方法时,保留计数已经为0,您的应用程序将引发异常。

您需要了解对象所有权规则。在手动保留和释放环境中(与ARC相反),Objective-C对象的规则可以用缩写词NARC记住

如果您有对使用newalloc创建的新对象的引用,或对调用retain的任何对象的引用,或对调用copymutableCopy创建的任何对象的引用,或者由以这些名称开头的任何方法创建的任何对象(例如,
newInstance
),然后 您拥有该对象,在解除分配之前,您必须释放该对象

但是,如果您没有做任何这些事情,那么您就不拥有该对象,并且不能释放它

在您的例子中,您没有在设置值的位置显示代码
searchField
,但是从崩溃中,我可以想象您从未获得对象的所有权。显示分配给
searchField
的代码,我们都会确定。
编辑:您可以通过使用
alloc
创建它来获得所有权。您需要将
释放
保存在
解除锁定
中。它是从别的地方释放出来的吗?或者单元格本身是否可能被过度释放?

如果对象在没有自动释放的情况下使用它会发生什么?当然,对象的接收者(使用代码返回值的函数/对象)有责任在使用后释放它。我有点误读了您的代码:您正在发布
searchField
,但没有显示如何创建它。[UISearchBar respondsToSelector:]:消息已发送到deallocated实例