为什么这个对象是潜在的泄漏?

发布于 2024-11-14 04:56:29 字数 1186 浏览 3 评论 0原文

我该如何解决这个潜在的泄漏问题?

- (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 的两个问题。

  1. 当前不拥有的对象的引用计数的减量不正确。
    为什么这个对象不属于我?

  2. 对象的潜在泄漏..(章节)
    如何正确释放?
    [章节自动释放]] ?

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.

  1. Incorrect decrement of the reference count of an object that is not owned at this point.
    Why is this object not owned by me ?

  2. Potential leak of an object.. (chapter)
    How release it correctly?
    [chapter autorelease]] ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-11-21 04:56:29

您不应该分配以下语句中的章节。

Chapter *chapter =[Chapter alloc] ;

请改用下面的。

 Chapter *chapter = nil;

我修改了你的代码

NSUInteger row = [indexPath row];
 Chapter *chapter = nil;

switch (indexPath.section) {
    case 0: 
        chapter = [[einfuerung objectAtIndex:row] retain];
        break;
    case 1: 
        chapter = [[vertiefung objectAtIndex:row] retain];
        break;
    case 2: 
        chapter = [[spezial objectAtIndex:row] retain];
        break;
   default:
        chapter =[[Chapter alloc] init];
        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];

}

You should not alloc the chapter in below statement.

Chapter *chapter =[Chapter alloc] ;

Use below instead.

 Chapter *chapter = nil;

I have modified your code

NSUInteger row = [indexPath row];
 Chapter *chapter = nil;

switch (indexPath.section) {
    case 0: 
        chapter = [[einfuerung objectAtIndex:row] retain];
        break;
    case 1: 
        chapter = [[vertiefung objectAtIndex:row] retain];
        break;
    case 2: 
        chapter = [[spezial objectAtIndex:row] retain];
        break;
   default:
        chapter =[[Chapter alloc] init];
        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];

}
纸短情长 2024-11-21 04:56:29
Chapter *chapter =[Chapter alloc];

您尚未发送init,但这不是泄漏的原因。问题出在开关盒上。

chapter = [einfuerung objectAtIndex:row];

当您执行此操作时,您将指向一个新的 chapter 对象,并且先前分配的对象将被泄漏。如果您总是从数组中获取 Chapter 对象(即最多有三个部分),那么您不需要 alloc。只需声明它,然后您也不需要发布。

Chapter *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 a Chapter object from an array (i.e. you have at most three sections) then you don't need the alloc. Just declare it, and you don't need to release then too.

一瞬间的火花 2024-11-21 04:56:29

您首先分配一个对象并将其分配给chapter。您忘记初始化它,但这不是问题所在。

当您覆盖 switch 语句中的 chapter 时,就会出现问题。对先前分配的对象的引用丢失,并且对象因此泄漏。

您需要做两件事:

  1. Chapter *chapter = nil;
  2. 删除最后的 [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:

  1. Chapter *chapter = nil;
  2. Remove the [chapter release]; at the end since you are not the owner of the elements returned by [someArray objectAtIndex:row].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文