【问题标题】:NSMutableArray addObject in for loop - memory leakfor循环中的NSMutableArray addObject - 内存泄漏
【发布时间】:2011-07-08 00:53:42
【问题描述】:

我将字符串(即某个目录中文件的文件名)放入带有 for 循环的 NSMutableArray 中: h 文件:

#import <Three20/Three20.h>

@interface AlbumController : TTThumbsViewController {
    NSMutableArray *images;
}

@property (nonatomic, retain) NSMutableArray *images;

@end

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

我在模拟器上没有任何问题,但在我的 iPod 上...

错误信息:

数据格式暂时不可用,将在“继续”后重试。 (加载共享库“/Developer/usr/lib/libXcodeDebuggerSupport.dylib”时出现未知错误)

提前致谢

【问题讨论】:

    标签: objective-c ios4 memory-leaks for-loop nsmutablearray


    【解决方案1】:

    我认为你应该使用 mutableCopy 而不是在你的 pics 数组上复制。

    所以而不是: NSMutableArray *pics = [[onlyJPGs copy] autorelease]; 你应该使用: NSMutableArray *pics = [[onlyJPGs mutableCopy] autorelease];

    在这个问题中有关复制/可变复制的更多信息:Copy & mutableCopy?

    【讨论】:

      【解决方案2】:

      看起来主要问题在于

       [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];
      

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

      否则,如果它已经初始化,则可能会发生内存泄漏,并且您可能不希望它自动释放;

      【讨论】:

      • 是的,好地方。它在模拟器中工作的原因是因为您在计算机上浪费的内存比在 iPhone 上要多得多。
      • 再次更新...我只是不明白问题是什么...同样的问题:(
      • 通过 SO 很难调试 ;-),这些问题会导致问题,但可能不是您看到的问题。至少有 2 个其他类我们无权访问“Photo”和“.photoSource”。那里也可能有问题。尝试对设备本身进行注释或调试,以追踪崩溃发生的确切位置。抱歉,我无法提供更多帮助。一段时间后,这一切都会成为你的第二天性
      • @Liam:当(retain) 属性已经有值时分配它不会导致内存泄漏,现有值被正确释放。这就是为什么你经常在viewDidUnload 方法中看到self.foo = nil
      • @Jim,说得好。作为参考...stackoverflow.com/questions/1458178/…
      【解决方案3】:

      您的 NSMutableArray 实例已自动释放。您将其分配给images ivar。您已将其声明为保留属性这一事实并不重要,因为您没有将其分配给该属性。我的猜测是您的意思是分配给该属性,而崩溃是由无意的释放引起的。

      变化:

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

      ...到:

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

      ...或:

      images = [[NSMutableArray alloc] init];
      

      另请注意,当您分配 NSMutableArray 的实例时,您的属性被声明为 NSArray

      另见the Memory Management Programming Guide

      【讨论】:

      • 第一个赋值给名为images的实例变量。第二个分配给名为images 的属性。后者是调用[self setImages:...] 的简写。此方法在您@synthesise 属性时创建,当属性声明为(retain) 时,setImages 方法为您设置 ivar 然后保留它。见:developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/…
      • 将其更改为:self.images = [[[NSMutableArray alloc] init] autorelease]; ...但同样的问题:(有什么帮助吗?
      • 你解决了NSArray/NSMutableArray之间的不匹配问题吗?
      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 2011-03-06
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多