我是一名嘗試爲iphone製作遊戲應用程序的初學者級別程序員,並且迄今爲止我遇到了我的程序的內存管理(exc_bad_access)可能存在的問題。我搜索並閱讀了許多關於內存管理的文章(包括蘋果的文檔),但我仍然無法弄清楚我的代碼究竟出了什麼問題。所以如果有人能幫助我清理我爲自己製造的混亂,我會非常感激。在我的遊戲模型中需要內存管理問題的幫助
//in the .h file
@property(nonatomic,retain) NSMutableArray *fencePoleArray;
@property(nonatomic,retain) NSMutableArray *fencePoleImageArray;
@property(nonatomic,retain) NSMutableArray *fenceImageArray;
//in the .m file
- (void)viewDidLoad {
[super viewDidLoad];
self.gameState = gameStatePaused;
fencePoleArray = [[NSMutableArray alloc] init];
fencePoleImageArray = [[NSMutableArray alloc] init];
fenceImageArray = [[NSMutableArray alloc] init];
mainField = CGRectMake(10, 35, 310, 340);
..........
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}
所以基本上,玩家觸摸屏幕來設置圍欄/極
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(.......) {
.......
}
else {
UITouch *touch = [[event allTouches] anyObject];
currentTapLoc = [touch locationInView:touch.view];
NSLog(@"%i, %i", (int)currentTapLoc.x, (int)currentTapLoc.y);
if(CGRectContainsPoint(mainField, currentTapLoc)) {
if([self checkFence]) {
onFencePole++;
//this 3 set functions adds their respective objects into the 3 NSMutableArrays using addObject:
[self setFencePole];
[self setFenceImage];
[self setFencePoleImage];
.......
}
}
else {
.......
}
}
}
}
的setFence功能(setFenceImage和setFencePoleImage與此類似)
-(void)setFencePole {
Fence *fencePole;
if (!elecFence) {
fencePole = [[Fence alloc] initFence:onFencePole fenceType:1 fencePos:currentTapLoc];
}
else {
fencePole = [[Fence alloc] initFence:onFencePole fenceType:2 fencePos:currentTapLoc];
}
[fencePoleArray addObject:fencePole];
[fencePole release];
,每當我按下游戲中的按鈕,調用endOpenState來清除屏幕上的所有額外圖像(柵欄/極點),並刪除3個NSMutableArray中的所有現有對象。點是刪除NSMutableArrays中的所有對象,但保留數組本身,以便稍後可以重用。
-(void)endOpenState {
........
int xMax = [fencePoleArray count];
int yMax = [fenceImageArray count];
for (int x = 0; x < xMax; x++) {
[[fencePoleImageArray objectAtIndex:x] removeFromSuperview];
}
for (int y = 0; y < yMax; y++) {
[[fenceImageArray objectAtIndex:y] removeFromSuperview];
}
[fencePoleArray removeAllObjects];
[fencePoleImageArray removeAllObjects];
[fenceImageArray removeAllObjects];
........
}
崩潰發生在checkFence函數的這裏。
-(BOOL)checkFence {
if (onFencePole == 0) {
return YES;
}
else if (onFencePole >= 1 && onFencePole < currentMaxFencePole - 1) {
CGPoint tempPoint1 = currentTapLoc;
CGPoint tempPoint2 = [[fencePoleArray objectAtIndex:onFencePole-1] returnPos]; // the crash happens at this line
if ([self checkDistance:tempPoint1 point2:tempPoint2]) {
return YES;
}
else {
return NO;
}
}
else if (onFencePole == currentMaxFencePole - 1) {
......
}
else {
return NO;
}
}
所以這裏的問題是,一切工作正常,直到checkFence調用endOpenState後第二次調用。因此,它像tap_screen - > tap_screen - > press_button_to_call_endOpenState - >自來水屏幕 - > tap_screen - >崩潰
的我在想什麼是fencePoleArray弄亂,當我用[fencePoleArray removeAllObjects],因爲它不會崩潰的時候我評論一下。如果有人能向我解釋出了什麼問題,那真的很棒。並提前致謝。
你在endOpenState復位onFencePole和currentMaxFencePole?如果不是,它看起來像你的代碼可以訪問(現在是空的)數組末尾的東西。 – user308405 2010-04-05 06:34:44
這會拋出異常,不是嗎? – zoul 2010-04-05 06:41:27
是的,我重置onFencePole和currentMaxFencePole。 – Treon 2010-04-05 07:14:58