开发者

NSMutableArray property getting autoreleased?

I have an NSMutableArray property defined in my Brain model.

@property (retain, nonatomic) NSMutableArray *stockColors;

The mutable array gets filled with objects durin开发者_开发百科g the Brain init.

In my view controller viewDidLoad function I do

*Brain brain = [[Brain alloc]init];
NSLog(@"Brain stockColors is %@", brain.stockColors);

This works well, and it logs a memory address as expected.

Then in the same view controller I have an "addButtonPressed" action declared linked to a UIButton in the .xib.

In my addbuttonPressed action if I try and do the exact same log

NSLog(@"Brain stockColors is %@", brain.stockColors);

The program crashes!

If I do

NSLog(@"Brain stockColors is %@", brain);

I get an address for the brain... so the brain exists.. but for some reason it seems that brain.stockColors is getting released or something by the time I get to the addButtonPressed action.

Can anyone explain what would cause it to get autoreleased?

Thanks


Considering you left out the important code... the actual Brain init call I am going to say that when you create your mutable array you are assigning to the instance variable and not the property.

//The Brain init method should contain something similar to this
self.stockColor = [NSMutableArray array];

//stockColor = [NSMutableArray array]; would be incorrect and get autoreleased
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜