【发布时间】: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 文档,但我找不到我在那里做错了什么……知道吗??
干杯,
提比。
【问题讨论】:
-
imageName在textures被释放之前不会被释放。你确定textures最终会被释放吗? -
哦,不应该是
[textures addObject:pics]吗?textures将在添加时自动保留图片 - 据我所知,您不会将pics的所有权传递给其他任何东西。
标签: objective-c iphone nsstring