NSArray вызывает EXC_BAD_ACCESS - PullRequest
       8

NSArray вызывает EXC_BAD_ACCESS

0 голосов
/ 06 марта 2012

Я объявляю NSArray в одном классе следующим образом:

.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;
}
}

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

1 Ответ

3 голосов
/ 06 марта 2012

Это проблема с памятью

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

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

componentsSeparatedByString: возвращает объект автоматического освобождения
, так как вы объявили массив как свойство retain, вы должны обновить его так:

self.listOfProfiles = [result componentsSeparatedByString:@"^-^"];
...