0
NSObject를 서브 클래스로하는 HighscoresController라는 클래스를 생성하려고합니다. 다음과 같은 방식으로 init
메서드를 호출하면 디버거에서 오류가 발생합니다 GDB: Program received signal: "EXC_BAD_ACCESS"
. 왜 아무 생각 없어? 나는 완전히 비틀 거린다. 메모리 누수 - 당신은 이전 방법에 할당 된 배열에 대한 참조를 잃게NSObject를 서브 클래 싱하는 중 오류 : "EXC_BAD_ACCESS"
_highscoresList = [HighscoresListOfLists objectAtIndex:kMyListNumber];
:
#import "HighscoresController.h"
#import "Constants.h"
@implementation HighscoresController
@synthesize highscoresList = _highscoresList;
- (id) init {
self = [super init];
_highscoresList = [[NSMutableArray alloc] initWithCapacity:kHighscoresListLength];
int kMyListNumber = 0;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"highscores.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { // if settings file exists
NSArray *HighscoresListOfLists = [[NSArray alloc] initWithContentsOfFile:filePath];
_highscoresList = [HighscoresListOfLists objectAtIndex:kMyListNumber];
[HighscoresListOfLists release];
} else { // if no highscores file, create a new one
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:_highscoresList];
[array writeToFile:filePath atomically:YES];
[array release];
}
[_highscoresList addObject:[NSNumber numberWithFloat:0.0f]];
return self;
}
- (void) addScore:(float)score {
// Implementation
}
- (BOOL) isScore:(float)score1 betterThan:(float)score2 {
if (score1 > score2)
return true;
else
return false;
}
- (BOOL) checkScoreAndAddToHighscoresList:(float)score {
NSLog(@"%d",[_highscoresList count]);
if ([_highscoresList count] < kHighscoresListLength) {
[self addScore:score];
[self saveHighscoresList];
return true;
} else {
NSNumber *lowScoreNumber = [_highscoresList objectAtIndex:[_highscoresList count]-1];
float lowScore = [lowScoreNumber floatValue];
if ([self isScore:score betterThan:lowScore]) {
[self addScore:score];
[self saveHighscoresList];
return true;
}
}
return false;
}
- (void) saveHighscoresList {
// Implementation
}
- (void) dealloc {
[_highscoresList release];
_highscoresList = nil;
[super dealloc];
}
@end
도움을 많이 주셔서 감사합니다. 정확하게 맞았습니다. 나는 그 라인을 다음으로 대체했다 : _highscoresList = [[NSMutableArray alloc] initWithArray : [HighscoresListOfLists objectAtIndex : kMyListNumber]]; 또한 _highscoresList에 대해 이전에 만든 첫 번째 할당을 제거했습니다. – jonsibley