为什么这个对象是潜在的泄漏?
我该如何解决这个潜在的泄漏问题?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
Chapter *chapter =[Chapter alloc] ;
switch (indexPath.section) {
case 0:
chapter = [einfuerung objectAtIndex:row];
break;
case 1:
chapter = [vertiefung objectAtIndex:row];
break;
case 2:
chapter = [spezial objectAtIndex:row];
break;
}
if ([[NSFileManager defaultManager] fileExistsAtPath:[chapter urlOnFilesystem]]) {
dataInstance.chapter = chapter;
Container *container = [[Container alloc] init];
[self.navigationController pushViewController:container animated:YES];
[container release];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Kapitel nicht vorhanden" message:@"Kapitel wurde noch nicht heruntergeladen" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
[chapter release];
}
Xcode 告诉我 Chapter 的两个问题。
当前不拥有的对象的引用计数的减量不正确。
为什么这个对象不属于我?对象的潜在泄漏..(章节)
如何正确释放?
[章节自动释放]] ?
how can i solve this potential leak ?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
Chapter *chapter =[Chapter alloc] ;
switch (indexPath.section) {
case 0:
chapter = [einfuerung objectAtIndex:row];
break;
case 1:
chapter = [vertiefung objectAtIndex:row];
break;
case 2:
chapter = [spezial objectAtIndex:row];
break;
}
if ([[NSFileManager defaultManager] fileExistsAtPath:[chapter urlOnFilesystem]]) {
dataInstance.chapter = chapter;
Container *container = [[Container alloc] init];
[self.navigationController pushViewController:container animated:YES];
[container release];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Kapitel nicht vorhanden" message:@"Kapitel wurde noch nicht heruntergeladen" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
[chapter release];
}
Xcode tells me two problems with chapter.
Incorrect decrement of the reference count of an object that is not owned at this point.
Why is this object not owned by me ?Potential leak of an object.. (chapter)
How release it correctly?
[chapter autorelease]] ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该分配以下语句中的章节。
请改用下面的。
我修改了你的代码
You should not alloc the chapter in below statement.
Use below instead.
I have modified your code
您尚未发送
init
,但这不是泄漏的原因。问题出在开关盒上。chapter = [einfuerung objectAtIndex:row];
当您执行此操作时,您将指向一个新的
chapter
对象,并且先前分配的对象将被泄漏。如果您总是从数组中获取Chapter
对象(即最多有三个部分),那么您不需要alloc
。只需声明它,然后您也不需要发布。You have not sent
init
but that is not the reason of leak. The problem is in the switch-case.chapter = [einfuerung objectAtIndex:row];
When you are doing this you are pointing to a new
chapter
object, and the previous alloced one is leaked. If you are always getting aChapter
object from an array (i.e. you have at most three sections) then you don't need thealloc
. Just declare it, and you don't need to release then too.您首先分配一个对象并将其分配给
chapter
。您忘记初始化它,但这不是问题所在。当您覆盖 switch 语句中的
chapter
时,就会出现问题。对先前分配的对象的引用丢失,并且对象因此泄漏。您需要做两件事:
Chapter *chapter = nil;
[chapter release];
因为您不是[someArray objectAtIndex:row]
。You first allocate an object and assign it to
chapter
. You forgot to initialize it, but that's not the problem.The problem arises when you overwrite
chapter
in your switch statements. The reference to the previously allocated object is lost and the object thus leaked.You need to do two things:
Chapter *chapter = nil;
[chapter release];
at the end since you are not the owner of the elements returned by[someArray objectAtIndex:row]
.