【问题标题】:NSString memory leak?NSString 内存泄漏?
【发布时间】:2010-08-08 12:53:28
【问题描述】:

我正在使用下面的代码来回转动,这给了我 pics 对象中的内存泄漏,显然链接到对象 imageName。

for (int i = 0;i<[potatoesIndexesArray count];i++){ 

    int imageNumber = [[potatoesIndexesArray objectAtIndex:i]intValue];

    NSString *imageName = [[NSString alloc] initWithFormat:@"texture%d",imageNumber];

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
    //UIImage *imageHighlighted = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];

    NSArray *pics = [[NSArray alloc] initWithObjects:
                     [self maskImage:image withMask:[mainDelegate.masksArray objectAtIndex:i]],
                     [self maskImage:image withMask:[mainDelegate.masksArray objectAtIndex:i]],
                     imageName, 
                      nil]; // pics becomes owner of objects

    [textures addObject:[pics retain]]; //textures becomes owner of pics. as a release occurs later. we must retaint pics to keep it available in textures.

    [imageName release];
    [image release];
    [pics release];

    //[imageHighlighted release];

}

我已经阅读了有关内存管理的 Apple 文档,但我找不到我在那里做错了什么……知道吗??

干杯,

提比。

【问题讨论】:

  • imageNametextures 被释放之前不会被释放。你确定textures 最终会被释放吗?
  • 哦,不应该是[textures addObject:pics]吗? textures 将在添加时自动保留图片 - 据我所知,您不会将 pics 的所有权传递给其他任何东西。

标签: objective-c iphone nsstring


【解决方案1】:

如果纹理是 NSMutableArray,那么您的 [textures addObject:] 调用已经向图片发送了保留。所以,代码应该是:

[textures addObject:pics];

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 2012-10-29
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多