Objective c 循环的NSMutableArray addObject-in-内存泄漏
我将字符串(某个目录中文件的文件名)放入带有for循环的NSMutableArray中: h文件:Objective c 循环的NSMutableArray addObject-in-内存泄漏,objective-c,ios4,memory-leaks,for-loop,nsmutablearray,Objective C,Ios4,Memory Leaks,For Loop,Nsmutablearray,我将字符串(某个目录中文件的文件名)放入带有for循环的NSMutableArray中: h文件: #导入 @接口控制器:TTThumbsViewController{ NSMutableArray*图像; } @属性(非原子,保留)NSMutableArray*图像; @结束 m文件: #import "AlbumController.h" #import "PhotoSource.h" #import "Photo.h" @implementation AlbumController @s
#导入
@接口控制器:TTThumbsViewController{
NSMutableArray*图像;
}
@属性(非原子,保留)NSMutableArray*图像;
@结束
m文件:
#import "AlbumController.h"
#import "PhotoSource.h"
#import "Photo.h"
@implementation AlbumController
@synthesize images;
-(void)createPhotos {
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error:nil];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];
NSMutableArray *pics = [[onlyJPGs copy] autorelease];
if(!self.images) {
self.images = [[NSMutableArray alloc] init];
}
for(int i = 0; i < [onlyJPGs count]; i++)
{
//NSLog([pics objectAtIndex:i]);
NSString *ImgURL = [@"bundle://" stringByAppendingString:[pics objectAtIndex:i]];
Photo *photo = [[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)];
[images addObject:photo];
[photo release];
}
}
-(void)viewDidLoad{
[self createPhotos]; // method to set up the photos array
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:@"Chili Pflanzen"
photos:images
photos2:nil
];
}
@end
#导入“AlbumController.h”
#导入“PhotoSource.h”
#导入“Photo.h”
@实现控制器
@合成图像;
-(作废)创建照片{
NSString*bundleRoot=[[NSBundle mainBundle]bundlePath];
NSArray*dirContents=[[NSFileManager defaultManager]目录目录路径:bundleRoot错误:nil];
NSArray*onlyJPGs=[dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@“self ENDSWITH”.jpg'”];
NSMUTABLEARRY*pics=[[仅JPGS复制]自动释放];
如果(!self.images){
self.images=[[NSMutableArray alloc]init];
}
对于(int i=0;i<[仅JPGS计数];i++)
{
//NSLog([pics对象索引:i]);
NSString*ImgURL=[@“bundle://”stringByAppendingString:[pics objectAtIndex:i]];
Photo*Photo=[[Photo alloc]initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320212)];
[图片添加对象:照片];
[图片发布];;
}
}
-(无效)viewDidLoad{
[self createPhotos];//设置照片数组的方法
self.photoSource=[[photoSource alloc]
initWithType:PhotoSourceNormal
标题:@“Chili Pflanzen”
照片:图片
照片2:无
];
}
@结束
我在模拟器中没有任何问题,但在我的iPod上
错误消息:
数据格式化程序暂时不可用,将在“继续”后重试。(加载共享库“/Developer/usr/lib/libxcodeduggersupport.dylib”时出现未知错误)
提前感谢您的
NSMutableArray
实例已自动删除。您正在将其分配给图像
ivar。将其声明为保留属性并不重要,因为您没有将其分配给属性。我的猜测是,您打算分配给该属性,而崩溃是由于无意中解除分配造成的
更改:
images = [[[NSMutableArray alloc] init] autorelease];
…致:
self.images = [[[NSMutableArray alloc] init] autorelease];
…或:
images = [[NSMutableArray alloc] init];
还请注意,在分配NSMutableArray
的实例时,您的属性被声明为NSArray
另请参见。看起来主要问题在于
[images addObject:[[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)]];
在这里,您正在分配照片,但没有发布它。将对象添加到数组时,会增加该对象的保留计数
试着把它改成
Photo *photo = [[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)];
[images addObject:photo];
[photo release];
此外 我要换衣服
self.images = [[[NSMutableArray alloc] init] autorelease];
到
否则,如果内存已经初始化,可能会发生内存泄漏,并且您可能不希望它自动释放 我认为您应该使用mutableCopy,而不是在pics阵列上进行复制 因此,不是: NSMUTABLEARRY*pics=[[仅JPGS复制]自动释放]; 你应使用: NSMUTABLEARRY*pics=[[仅JPGS mutableCopy]自动释放]
关于这个问题中的copy/mutablecopy的更多信息:第一个赋值给名为
images
的实例变量。第二个属性指定给名为images
的属性。后者是调用[self-setImages:…]
的缩写。此方法是在@合成属性时创建的,当属性声明为(retain)
时,setImages
方法为您设置ivar,然后保留它。请参阅:将其更改为:self.images=[[[NSMutableArray alloc]init]autorelease]代码>。。。但同样的问题:(有什么帮助吗?你修复了NSArray
/NSMutableArray
之间的不匹配吗?是的,很好。它在模拟器中工作的原因是因为你在电脑上比在iPhone上浪费的内存多得多。再次更新它…我不明白问题是什么。同样的问题:(这样很难调试;-),这些问题可能会导致问题,但可能不是您看到的问题。至少还有两个类我们无法访问“Photo”和“.photoSource”。那里可能也有问题。请尝试在设备上进行注释或调试,以跟踪崩溃发生的确切位置。很抱歉,我无法提供更多信息救命啊,过一段时间,这一切都会成为你的第二天性you@Liam:当(retain)
属性已经有值时,分配给该属性不会导致内存泄漏,现有值已被正确释放。这就是为什么您经常在viewDidUnload
方法中看到self.foo=nil
的原因。@Jim,说得好。作为参考。。。
self.images = [[[NSMutableArray alloc] init] autorelease];
if(!self.images) {
self.images = [[NSMutableArray alloc] init];
}