我是一名尝试为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