Iphone 为什么这种版本不';不行?
我有一个关于以下内容的新手问题:Iphone 为什么这种版本不';不行?,iphone,objective-c,xcode,memory-management,Iphone,Objective C,Xcode,Memory Management,我有一个关于以下内容的新手问题: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSArray *anArray; anArray = [dictionary objectForKey: [NSString stringWithFormat:@"%d", section]]; //here dictionary is of type
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *anArray;
anArray = [dictionary objectForKey: [NSString stringWithFormat:@"%d", section]];
//here dictionary is of type NSDictionary, initialized in another place.
AnObject *obj = [[AnObject alloc] init];
obj = [anArray objectAtIndex:0];
[anArray release];
return obj.title;
}
如果我按原样运行它,我将得到一个错误。
如果我不发布[anArray release],它就可以正常工作。
我不太明白为什么会这样
谢谢。您不需要释放anArray,因为您没有创建它。这本字典只是给你一个指向它的指针 看起来,在创建对象的地方确实存在内存泄漏。在下一行中,您重新分配变量“obj”,使其成为从anArray获得的内容。但您尚未释放在上面一行中创建的AnObject 我认为您的代码应该如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *anArray;
anArray = [dictionary objectForKey: [NSString stringWithFormat:@"%d", section]];
//here dictionary is of type NSDictionary, initialized in another place.
obj = [anArray objectAtIndex:0];
return obj.title;
}
您不需要释放尚未创建的内容。您不需要释放阵列,因为您没有创建它。这本字典只是给你一个指向它的指针 看起来,在创建对象的地方确实存在内存泄漏。在下一行中,您重新分配变量“obj”,使其成为从anArray获得的内容。但您尚未释放在上面一行中创建的AnObject 我认为您的代码应该如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *anArray;
anArray = [dictionary objectForKey: [NSString stringWithFormat:@"%d", section]];
//here dictionary is of type NSDictionary, initialized in another place.
obj = [anArray objectAtIndex:0];
return obj.title;
}
您不需要发布尚未创建的内容。
anArray
不由您分配、保留、复制或新建,因此您不需要发布它
此外,还有一个漏洞,您使用alloc/init创建了一个全新的AnObject
实例,但随后从数组中为其分配了一个新值
您的代码应该如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *anArray;
anArray = [dictionary objectForKey: [NSString stringWithFormat:@"%d", section]];
//here dictionary is of type NSDictionary, initialized in another place.
AnObject *obj = [anArray objectAtIndex:0];
return obj.title;
}
anArray
不是您分配、保留、复制或新建的,因此您不需要发布它
此外,还有一个漏洞,您使用alloc/init创建了一个全新的AnObject
实例,但随后从数组中为其分配了一个新值
您的代码应该如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *anArray;
anArray = [dictionary objectForKey: [NSString stringWithFormat:@"%d", section]];
//here dictionary is of type NSDictionary, initialized in another place.
AnObject *obj = [anArray objectAtIndex:0];
return obj.title;
}
你绝对必须阅读并理解,尤其是基本规则,其中规定: 如果您使用名称以“alloc”或“new”开头或包含“copy”的方法(例如,alloc、newObject或mutableCopy)创建对象,或者向其发送retain消息,则您将获得对象的所有权。您负责放弃使用“释放”或“自动释放”拥有的对象的所有权。在任何其他时间收到对象时,都不能释放它 现在,看看你是怎么抓住anArray的。您使用了objectForKey方法:它既不以alloc开头,也不以new开头,也不包含copy。你也没有保留混乱。因此,您不拥有anArray。你不能释放它 上面引用的规则是关于在iPhone或Mac上使用Cocoa编程而不进行垃圾收集的最重要的一点。这里的另一张海报上出现了缩写词NARC(New Alloc Retain Copy),作为一份备忘录,非常方便
让我们将规则应用于代码中名为obj的变量。您通过调用alloc获得了它,因此您有责任发布它。但是,您随后通过调用objectForIndex再次获得它(覆盖以前的值):因此在此之后,您不能释放它。然而,第一个值确实需要释放,现在已经泄漏。事实上,alloc行是不必要的。你绝对必须阅读并理解,尤其是基本规则,其中规定: 如果您使用名称以“alloc”或“new”开头或包含“copy”的方法(例如,alloc、newObject或mutableCopy)创建对象,或者向其发送retain消息,则您将获得对象的所有权。您负责放弃使用“释放”或“自动释放”拥有的对象的所有权。在任何其他时间收到对象时,都不能释放它 现在,看看你是怎么抓住anArray的。您使用了objectForKey方法:它既不以alloc开头,也不以new开头,也不包含copy。你也没有保留混乱。因此,您不拥有anArray。你不能释放它 上面引用的规则是关于在iPhone或Mac上使用Cocoa编程而不进行垃圾收集的最重要的一点。这里的另一张海报上出现了缩写词NARC(New Alloc Retain Copy),作为一份备忘录,非常方便
让我们将规则应用于代码中名为obj的变量。您通过调用alloc获得了它,因此您有责任发布它。但是,您随后通过调用objectForIndex再次获得它(覆盖以前的值):因此在此之后,您不能释放它。然而,第一个值确实需要释放,现在已经泄漏。事实上,alloc行是不必要的。似乎我们在同一时间发布了几乎完全相同的答案;)似乎我们在同一时间发布了几乎完全相同的答案;)呵呵,你是说这不是一场比赛:-)呵呵,你是说这不是一场比赛:-)我的回答有点不准确。关于在非GC环境中编程Cocoa,引用的规则是最重要的(iPhone是非GC的)。我的回答有点不准确。关于在非GC环境中编程Cocoa,引用的规则是最重要的。但是,您也在不必要地分配
对象的一个实例,然后在下一行泄漏它。但是,您也在不必要地分配AnObject
的实例,然后在下一行泄漏它。