开发者

NSMutableArray addObject in for loop - memory leak

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 开发者_JS百科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


I think you should use mutableCopy and not copy on your pics array.

so instead of: NSMutableArray *pics = [[onlyJPGs copy] autorelease]; you should use: NSMutableArray *pics = [[onlyJPGs mutableCopy] autorelease];

More information about copy/mutablecopy in this question: Copy & mutableCopy?


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;


Your NSMutableArray instance is autoreleased. You are assigning it to the images ivar. The fact that you have declared it as a retained property doesn't matter, because you aren't assigning it to the property. My guess is that you meant to assign to the property, and the crash is caused by the inadvertent deallocation.

Change:

images = [[[NSMutableArray alloc] init] autorelease];

...to:

self.images = [[[NSMutableArray alloc] init] autorelease];

...or:

images = [[NSMutableArray alloc] init];

Also note that your property is declared as NSArray when you are allocating an instance of NSMutableArray.

Also see the Memory Management Programming Guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜