NSMutableArray addObject for for循环 - 内存泄漏 [英] NSMutableArray addObject in for loop - memory leak
问题描述
我把字符串(这是在一个特定的目录中的文件的文件名)放入一个NSMutableArray for循环:
h-file:
#import< Three20 / Three20.h>
@interface AlbumController:TTThumbsViewController {
NSMutableArray * images;
@property(nonatomic,retain)NSMutableArray * images;
@end
m-file:
#importAlbumController.h
#importPhotoSource.h
#importPhoto.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];
$ b $ for(int i = 0; i< [onlyJPGs count]; i ++)
{
// NSLog([pics objectAtIndex:i]) ;
NSString * ImgURL = [@bundle://stringByAppendingString:[pics objectAtIndex:i]];
照片* photo = [[照片分配] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320,212)];
[images addObject:photo];
[photo release];
- (void)viewDidLoad {
[self createPhotos]; //设置照片数组的方法
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:@Chili Pflanzen
photos:images
photos2:nil
];
}
@end
我没有任何问题在模拟器,但在我的iPod ...
错误消息:
数据FOrmatters暂时不可用,将重新在继续之后运行。 (未知的错误加载共享库/Developer/usr/lib/libXcodeDebuggerSupport.dylib)
提前致谢
看起来像是主要问题是
[images addObject:[[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320,212)]];
在这里您正在分配Photo但不会释放它。当你添加一个对象到一个数组时,它增加了它的保留计数。
尝试改变它到
Photo * photo = [[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320,212)];
[images addObject:photo];
[photo release];
另外...
我会改变
$ $ $ $ $ $ $ $> self.images = [[[[NSMutableArray alloc] init]自动释放];
到
if(!self.images){
self.images = [[NSMutableArray alloc] init];
}
如果已经存在内存泄漏已被初始化,以及你可能不希望它autoreleased;
i'm putting strings (which are filenames of files in a certain directory) into an NSMutableArray with a for loop: h-file:
#import <Three20/Three20.h>
@interface AlbumController : TTThumbsViewController {
NSMutableArray *images;
}
@property (nonatomic, retain) NSMutableArray *images;
@end
m-file:
#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
i do not have any problem in the simulator but on my iPod...
Error message:
Data FOrmatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
thanks in advance
Looks like the main issue is with
[images addObject:[[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)]];
Here you are alloc'ing Photo but not releasing it. When you add an object to an array it increases the retain count for it.
Try changing it to
Photo *photo = [[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)];
[images addObject:photo];
[photo release];
In addition ...
I'd change
self.images = [[[NSMutableArray alloc] init] autorelease];
to
if(!self.images) {
self.images = [[NSMutableArray alloc] init];
}
Otherwise there is the potential for a memory leak if it has already been initialized, as well as that you probably do not want it autoreleased;
这篇关于NSMutableArray addObject for for循环 - 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!