gpt4 book ai didi

arrays - 释放数组会使xCode中的应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 16:55:52 25 4
gpt4 key购买 nike

为什么释放数组时我的应用程序崩溃?香港专业教育学院一直试图找出解决办法!如果我不释放它,它将导致内存泄漏。如果我释放它,它会因以下错误而崩溃:
程序接收信号:“EXC_BAD_ACCESS”。
有时它也这样说:
暂时不可用的数据格式化程序,将在“继续”后重试。 (目前不安全调用dlopen。)

只是无法解决。有很多代码,所以我将粘贴(我认为)相关的部分。

-(void)startGame:(int)levelNumber {

//NOTE there is some code ive not pasted as its too big, but I dont think its relevant

levelsArray = [gameLevels loadLevelMap:levelNumber];
levelsArray_charGuards = [[levelsArray objectAtIndex:4]retain];


//CREATE CHAR MAIN ########
charMainStore = [[NSMutableArray arrayWithCapacity:1] retain];

charMainToDestroy = [[NSMutableArray arrayWithCapacity:1] retain];

CharMain* charMain = [[CharMain alloc] initWithFrame:CGRectMake(((levels_charStart_x)*40), ((levels_charStart_y)*40), 0, 0)];
[viewController.mapView addSubview:charMain];

[self registerMainChar:charMain];
charMain.gameController = self;
charMain.currentGrid_x = levels_charStart_x;
charMain.currentGrid_y = levels_charStart_y;
charMain.nextGrid_x = levels_charStart_x;
charMain.nextGrid_y = levels_charStart_y;
mapViewController.gameController = self;

[self moveMap_x:((levels_charStart_x)*40) moveMap_y:((levels_charStart_y)*40)];

[charMain release];

//CREATE CHAR GUARD ########
charGuardStore = [[NSMutableArray arrayWithCapacity:10] retain];
charGuardToDestroy = [[NSMutableArray arrayWithCapacity:10] retain];
//These are both @synthesize properly
//levelsArray_charGuards is from previously

for (NSMutableArray* levelsArray_charGuards_path in levelsArray_charGuards) {

NSMutableArray* levelsArray_charGuards_path_eachCoOrd = [levelsArray_charGuards_path objectAtIndex:0];

int levels_charGuardStart_x = [[NSString stringWithFormat:@"%@",[levelsArray_charGuards_path_eachCoOrd objectAtIndex:0]] intValue];
int levels_charGuardStart_y = [[NSString stringWithFormat:@"%@",[levelsArray_charGuards_path_eachCoOrd objectAtIndex:1]] intValue];

CharGuard* charGuard = [[CharGuard alloc] initWithFrame:CGRectMake((levels_charGuardStart_x*40), (levels_charGuardStart_y*40), 0, 0)];
//CharGuard* charGuard = [[CharGuard alloc] initWithFrame:CGRectMake(((levels_charStart_x)*40), ((levels_charStart_y)*40), 0, 0)];

[viewController.mapView addSubview:charGuard];

[self registerGuardChar:charGuard];
charGuard.gameController = self;
[charGuard setImage];
charGuard.currentGrid_x = levels_charGuardStart_x;
charGuard.currentGrid_y = levels_charGuardStart_y;
charGuard.nextGrid_x = levels_charGuardStart_x;
charGuard.nextGrid_y = levels_charGuardStart_y;
mapViewController.gameController = self;

charGuard.levelsArray_charGuards_path = levelsArray_charGuards_path;

[charGuard release];
charGuard = nil;


levelsArray_charGuards_path = nil;

levelsArray_charGuards_path_eachCoOrd =nil;
}
}

当一个级别完成或重新启动时,这称为:
-(void)clearLevel {
NSLog(@"Clear Level");

for (CharMain* charMain in charMainStore){

[charMain removeFromSuperview];
[charMainToDestroy addObject:charMain];
}

for (CharMain* charMain in charMainToDestroy){

[charMainStore removeObject:charMain];

}

for (CharGuard* charGuard in charGuardStore) {

[charGuard removeFromSuperview];
[charGuardToDestroy addObject:charGuard];

}

for (CharGuard* charGuard in charGuardToDestroy){

[charGuardStore removeObject:charGuard];

}

[charGuardStore release];
charGuardStore=nil;

[charMainStore release];
charMainStore=nil;

//[charGuardToDestroy release]; //If I allow this it crashes!***********
charGuardToDestroy= nil;

[charMainToDestroy release];
charMainToDestroy = nil;

}

然后,游戏再次调用startGame:(int)levelNumber。

我还有其他我未在此处显示的对象,但是它们都可以正常工作。它只是不想被释放的charGuardToDestroy!

我努力了:
-(void)clearLevel {
NSLog(@"Clear Level");

for (CharMain* charMain in charMainStore){

[charMain removeFromSuperview];
[charMainToDestroy addObject:charMain];
}

for (CharMain* charMain in charMainToDestroy){

[charMainStore removeObject:charMain];

}

for (CharGuard* charGuard in charGuardStore) {

[charGuard removeFromSuperview];
//[charGuardToDestroy addObject:charGuard];

}
/*
for (CharGuard* charGuard in charGuardToDestroy){

[charGuardStore removeObject:charGuard];

}
*/

[charGuardStore release];
charGuardStore=nil;

[charMainStore release];
charMainStore=nil;

[charGuardToDestroy release]; //I tried allowing this and removing the above code ***********
charGuardToDestroy= nil;

[charMainToDestroy release];
charMainToDestroy = nil;

}

这也有效,但是为什么呢?我确实需要能够从屏幕上以及从数组中删除charGuard,然后释放它,但是我只能做一个。真奇怪!

最佳答案

您很可能在某处向数组charGuardStore添加了CharGuard对象,该对象仅由该数组和charGuards super View 保留。

我认为这是发生的情况:

for (CharGuard* charGuard in charGuardStore) {

[charGuard removeFromSuperview]; // charGuard only retained by charGuardStore (= 1 retain left)
[charGuardToDestroy addObject:charGuard]; // charGuard retained by charGuardToDestroy too (= 2 retains left)

}

for (CharGuard* charGuard in charGuardToDestroy){

[charGuardStore removeObject:charGuard]; // remove 1 retain (= 1 retain left) because it's still in charGuardToDestroy

}

/* more code */

[charGuardToDestroy release]; // remove all charGuards. charGuard gets released. And most likely you access it later.

因此很可能在创建CharGuard时就已经发生了错误。可能过度释放,但效果要晚得多。

使用 NSZombieEnabled确定是否是这种情况。

我认为,如果从该数组中删除了charGuards之后添加 [charGuardToDestroy removeAllObjects];,它将崩溃。这将证实我所说的。

关于arrays - 释放数组会使xCode中的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9179026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com