NSMutableArray addObject in for loop - memory leak

I put the lines (which are the names of the files in a specific directory) in an NSMutableArray with a for: h file loop:

#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 have no problem in the simulator, but on my iPod ...

Error message:

Data FOrmatters are temporarily unavailable, try again after continuing. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")

early

0
source share
3 answers

It seems that the main problem is related to

 [images addObject:[[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)]];

Here you select a photo, but do not release it. When you add an object to an array, it increases its number.

Try changing it to

Photo *photo = [[Photo alloc] initWithURL:ImgURL smallURL:ImgURL size:CGSizeMake(320, 212)];
[images addObject:photo];
[photo release];

Besides...

I would change

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

to

if(!self.images) {
 self.images = [[NSMutableArray alloc] init];
}

, , , , ;

+1

, mutableCopy, pics.

:   NSMutableArray * pics = [[onlyJPGs copy] autorelease]; :   NSMutableArray * pics = [[onlyJPGs mutableCopy] autorelease];

copy/mutablecopy : mutableCopy?

+1

NSMutableArray . images ivar. , , , . , , .

:

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

... :

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

... :

images = [[NSMutableArray alloc] init];

Also note that your property is declared as NSArrayin instance distribution NSMutableArray.

Also see Memory Management Programming Guide .

0
source

All Articles