NSMutableArray addObject in for loop - memory leak

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 07:39:06

问题


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


回答1:


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?




回答2:


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;




回答3:


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.



来源:https://stackoverflow.com/questions/5180787/nsmutablearray-addobject-in-for-loop-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!