0

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

Liam
  • 7,762
  • 4
  • 26
  • 27
Chris
  • 200
  • 2
  • 4
  • 12

3 Answers3

1

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;

Liam
  • 7,762
  • 4
  • 26
  • 27
  • Yep, good spot. The reason why it works in the simulator is because you have a lot more memory to waste on your computer than you do on the iPhone. – Jim Mar 03 '11 at 13:45
  • updated it again...i just don't understand what the problem is..same issue :( – Chris Mar 03 '11 at 14:18
  • It's difficult to debug through SO ;-), these issues will have caused problems but may not be the problem you are seeing. There are at least 2 other classes that we don't have access to 'Photo' and the '.photoSource'. There may be a problem there also. Try commenting out things or debugging on the device itself to track down where exactly the crash is happening. Sorry I can't be more help. After a while it'll all be second nature to you – Liam Mar 03 '11 at 14:29
  • @Liam: Assigning to a `(retain)` property when it already has a value does not cause a memory leak, the existing value is correctly released. That's why you see `self.foo = nil` in `viewDidUnload` methods so often. – Jim Mar 03 '11 at 15:40
  • @Jim, good point. As a reference ... http://stackoverflow.com/questions/1458178/iphone-dealloc-release-vs-nil – Liam Mar 03 '11 at 15:57
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?

Community
  • 1
  • 1
0

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.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • The first assigns to the instance variable called `images`. The second assigns to the property called `images`. The latter is shorthand for calling `[self setImages:...]`. This method is created when you `@synthesise` the property, and when the property is declared as `(retain)`, the `setImages` method sets the ivar for you then retains it. See: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html%23//apple_ref/doc/uid/TP30001163-CH17 – Jim Mar 03 '11 at 13:26
  • changed it to: `self.images = [[[NSMutableArray alloc] init] autorelease];` ... but same issue :( any help? – Chris Mar 03 '11 at 13:26
  • Did you fix the mismatch between `NSArray`/`NSMutableArray`? – Jim Mar 03 '11 at 13:28