Hello, I have a custom class:
@interface Player : NSObject {
NSInteger mPlayerNo;
}
-(id) initWithNum:(NSInteger) playerNum;
@implementation Player
-(id) initWithNum:(NSInteger) playerNum
{
if(![super init])
return nil;
...
mPlayerNo=playerNum;
return self;
}
@end
I need the array of the Player objects in the other programm class:
@interface Spec : NSObject {
NSMutableArray * mPlayers;
...
}
So, I am trying to fill mPlayers arr in the init method of the Spec class like this:
- (id)init {
if(![super init])
return nil;
NSMutableArray * _array=[[NSMutableArray alloc] init];
mPlayers=_array;
[_array release];
Player * _player=[[[Player alloc] initWithNum:(NSInteger)1]autorelease];
[mPlayers addObject:_player]; // crashes with EXC_BAD_ACCESS
It doesn't work. But if I change
NSMutableArray * _array=[[NSMutableArray alloc] init];
mPlayers=_array;
[_array release];
with
mPlayers=[[NSMutableArray array]retain];
All works fine. It is something strange for me. Please, help to understand the problem with alloc init.