tags:

views:

1757

answers:

2

First of all many thanks for clicking into my question.

I have a few UIImageView and a NSMutableArray

at .h

{
    IBOutlet UIImageView *img1;
    IBOutlet UIImageView *img2;
    IBOutlet UIImageView *img3;
    NSMutable Array *aImg;
}
@property(nonatomic, retian) IBOutlet UIImageView *img1;
@property(nonatomic, retian) IBOutlet UIImageView *img2;
@property(nonatomic, retian) IBOutlet UIImageView *img3;
@property(nonatomic, retain) NSMutableArray *aImg;

at .m

-(void)viewDidLoad
{
    [super viewDidLoad];
    aImg = [NSMutableArray arrayWithObjects: img1, img2, img3, nil];
}

then at other functions, when I write

UIImageView *tmp1;
tmp1 = [aImg objectAtIndex:0];

it said GDB: Program loaded; and displayed some shit
0x92a1d688 <+0024> mov 0x20(%edx), %edi

Anyone could help me?

A: 

You want to retain the array you're creating, otherwise it is released immediately afterwards.

-(void)viewDidLoad{
    [super viewDidLoad];
    aImg = [[NSMutableArray arrayWithObjects: img1, img2, img3, nil] retain];
}
Adam Ernst
It's a little silly to use the autorelease version of a method, just to immediately retain it. [[NSMutableArray alloc] initWithObjects: img1, img2, img3, nil] will do this this cleanly without any needless retaining and releasing.
Squeegy
thanks for your answerI really hate stupid objective-c. Adding nil is non-sense, adding this retain is even more non-sense, I haven't experienced other language that ugly..... "componentsSeparatedByString" instead of "split" ? wtf
Unreality
It's different, but you do learn to appreciate it. Stick with it. XCode autocompletion will make gigantic method names not that big a deal.
Squeegy
+1  A: 

Was it an EXC BAD ACCESS error? I bet it was. This is caused when you try to access an object that has been deleted, or deallocated, from memory.

This line:

aImg = [NSMutableArray arrayWithObjects: img1, img2, img3, nil];

returns an array object set to autorelease. This means that after the event that caused this action finishes it will be released. If you don't specifically retain it, it will be released, the retain count will fall to zero, and the object will be deallocated.

Then later on when you try to use the array, its not actually there and the program crashes. If you do this instead:

aImg = [[NSMutableArray alloc] initWithObjects: img1, img2, img3, nil];

Then you are directly initializing an array, and this one will not be autoreleased. Just make sure that in the -(void)dealloc method of your class that holds this array you add:

[aImg release];

Read this document, then read it again. It's very important stuff for iPhone development. http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

Squeegy