我在一个类中声明了一个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;
}
}
我认为这可能是一个记忆问题,但我可能完全错了。
答案 0 :(得分: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:@"^-^"];