2012-03-05 9 views
0

я объявить NSArray в одном классе, как это:NSArray вызывает EXC_BAD_ACCESS

.h

@interface HTTP : NSObject { 
NSArray *listOfProfiles; 
} 
@property (nonatomic, retain) NSArray *listOfProfiles; 

.m

-(id) init { 
if ((self = [super init])) { 
    listOfProfiles = [[NSArray alloc] init]; 
} 
return self; 
} 

-(void) someMethod { 
... 
    case GET_LIST_OF_PROFILES: 
     listOfProfiles = [result componentsSeparatedByString:@"^-^"]; 
     NSLog(@"first break: %@",[listOfProfiles objectAtIndex:0]); 
     break; 
... 
} 

я могу получить доступ к нему здесь просто отлично, то при попытке доступа он в другом классе после создания объекта получает ошибку EXC_BAD_ACCESS, а отладчик отправляется на main.m:

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
http = [[HTTP alloc] init]; 
[http createProfileArray]; 
profileListDelay = [[NSTimer alloc] init]; 
profileListDelay = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(profileListSelector) userInfo:nil repeats:YES]; 

} 

- (void) profileListSelector 
{ 
if (http.activityDone) 
{ 
    // http.listofprofiles mem leak? 
    for (int i = 0; i < http.listOfProfiles.count; i++) 
    { 
     NSLog(@"%@",[http.listOfProfiles objectAtIndex:i]); 
    } 
    [profileListDelay invalidate]; 
    profileListDelay = nil; 
} 
} 

Я думаю, что это проблема с памятью, возможно, но я мог быть совершенно неправ.

ответ

3

Это проблема памяти

Он находится в someMethod

-(void) someMethod { 
... 
    case GET_LIST_OF_PROFILES: 
     listOfProfiles = [result componentsSeparatedByString:@"^-^"]; 
     NSLog(@"first break: %@",[listOfProfiles objectAtIndex:0]); 
     break; 
... 
} 

componentsSeparatedByString: возвращает autoreleased объект
Поскольку вы объявили массив как retain собственности, вам необходимо обновить его следующим образом:

self.listOfProfiles = [result componentsSeparatedByString:@"^-^"]; 
Смежные вопросы