2012-03-05 128 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:返回一個自動釋放的對象
既然你聲明的數組作爲retain屬性,你應該更新它像這樣:

self.listOfProfiles = [result componentsSeparatedByString:@"^-^"]; 
相關問題