Code:
// Controller.h
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject {
IBOutlet NSImageView *imageView;
NSMutableArray * images;
int currentImage;
}
- (IBAction)nextImage:(id)sender;
@end
// Controller.m
#import "Controller.h"
@implementation Controller
- (void)awakeFromNib
{
NSBundle * mainBundle = [NSBundle mainBundle];
NSArray * imagePaths = [mainBundle pathsForResourcesOfType:@"jpg"
inDirectory:nil];
images = [[NSMutableArray alloc] init];
int count = [imagePaths count];
int i;
for (i = 0; i < count; i++) {
NSImage * image = [[NSImage alloc] initWithContentsOfFile:
[imagePaths objectAtIndex:i]];
[images addObject:image];
[image release];
}
currentImage = 0;
[imageView setImage:[images objectAtIndex:currentImage]];
}
- (IBAction)nextImage:(id)sender
{
currentImage++;
if (currentImage == [images count]) { // a
currentImage = 0;
}
[imageView setImage:[images objectAtIndex:currentImage]]; // b
}
@end
My question is :
1 there is a instance variable that owned by this class. It is allocated in the awakefromnib method. Should I override the dealloc method to release this instance variable?
2 If I did so, it seems that the application did not invoke the dealloc method at all. I wonder why.
3 if i add a [images autorelease] in the awakefromnib method for later release, an error occurred when i hit the next button. That means the images has been released. but it should be added to the autoreleasepool and then get released when application terminate. things work properly in command line utility. why didn't it work here?