Xcode 11db 错误:线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32804751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 11db error thing: Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
提问by CharlieFan39
I've been trying to write a game in Xcode where a game board is drawn in the terminal using a multidimensional array of type char. The game is supposed to be like a dungeon where there are doors that you can step on and when you do a new room in generated. But sometimes I get this really annoying "(11db)" error thing where one of my lines gets highlighted saying:
我一直在尝试在 Xcode 中编写一个游戏,其中使用 char 类型的多维数组在终端中绘制游戏板。游戏应该像一个地牢,在那里你可以踩到门,当你在生成一个新房间时。但有时我会遇到这个非常烦人的“(11db)”错误,其中我的一行被突出显示说:
"Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)"
“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)”
It's not the first time I've had the (11db) error in Xcode and previously I've been able to fix it but I can't seem to this time. What does that error mean? What I find the most annoying part it that it doesn't happen every time. Sometimes it'll happen as soon as I run the program when the room is created, other times it happens after you go through a door and I've even been able to go without it. Here's the code where it's happening:
这不是我第一次在 Xcode 中遇到 (11db) 错误,之前我已经能够修复它,但这次我似乎无法修复。这个错误是什么意思?我觉得最烦人的部分是它不会每次都发生。有时它会在我创建房间时运行程序后立即发生,有时它会在你穿过一扇门之后发生,我什至可以没有它。这是发生的代码:
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 1);
//Check to see if door location is the same an an enemy or the player
for (int i = 0; i < enemies.size(); i++){
while ((door_x == enemies.at(i).getX() && door_y == enemies.at(i).getY()) || (door_x == player->getX() && door_y == player->getY())){
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 0);
}
}
room[door_y][door_x] = DOOR;//Place door on map
This error is happening on the last line of the above C++ code. What exactly is wrong and why?
此错误发生在上述 C++ 代码的最后一行。究竟有什么问题,为什么?
Here's the whole function that creates a new room and my random number function.
这是创建新房间的整个函数和我的随机数函数。
int randomNumber(int max, int min){
int randomNumber = rand() %(max - min) + min;
return randomNumber;
}
void create_new_room(){
//Get random room height and room width
height = randomNumber(settings[0], settings[1]);
width = randomNumber(settings[2], settings[3]);
room = new char*[height];
for (int iter = 0; iter != height; iter++) {
room[iter] = new char[width];
}
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
room[i][j] = EMPTY;
}
}
enemies.clear();//Delete every object from the vector
//Create the enemies
int numEnemies = randomNumber(settings[4], settings[5]);
for (int i = 0; i < numEnemies; i++){
int randomHeight = randomNumber(height, 0);
int randomWidth = randomNumber(width, 0);
Enemy *e;//Create pointer to class to allow vector class push back
e = new Enemy(randomWidth, randomHeight, HEALTH);
enemies.push_back(*e);
delete e;//Delete pointer e to free memory and avoid any memory leak.
room[enemies.at(i).getY()][enemies.at(i).getX()] = ENEMY;//Place enemy on board
}
//Create Player
int player_y = randomNumber(height, 0);
int player_x = randomNumber(width, 0);
for (int i = 0; i < enemies.size(); i++){
while (player_x == enemies.at(i).getX()){
player_y = randomNumber(height, 0);
player_x = randomNumber(width, 0);
}
}
player = new Player(player_x, player_y, ATTACK);
//Place player on board
room[player->getY()][player->getX()] = PLAYER;
//Create a door
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 1);
//Check to see if door location is the same an an enemy or the player
for (int i = 0; i < enemies.size(); i++){
while ((door_x == enemies.at(i).getX() && door_y == enemies.at(i).getY()) || (door_x == player->getX() && door_y == player->getY())){
door_x = randomNumber(height - 1, 1);
door_y = randomNumber(width - 1, 1);
}
}
room[door_y][door_x] = DOOR;//Place door on map
}
Settings[] is an array of setting that I have which reads in values from a text file with user settings for the max and min random values. 0 = Max height 1 = Min height 2 = Max width 3 = Min width 4 = Max enemy 5 = Min enemy
Settings[] 是我拥有的一组设置,它从文本文件中读取值,其中包含最大和最小随机值的用户设置。0 = 最大高度 1 = 最小高度 2 = 最大宽度 3 = 最小宽度 4 = 最大敌人 5 = 最小敌人
Also I use srand(time(0))
at the top of my main function.
我也在我srand(time(0))
的主要功能的顶部使用。
My code is a bit messy but here it is.
我的代码有点乱,但就是这样。
回答by Ewan Mellor
Your code is crashing because you are dereferencing a pointer and ending up in a bad bit of memory. EXC_I386_GPFLT
is basically Exception, i386 (your CPU architecture), General Protection Fault, which means that your code is trying to read or write memory that it shouldn't.
您的代码正在崩溃,因为您正在取消对指针的引用并最终导致内存不足。 EXC_I386_GPFLT
基本上是异常、i386(您的 CPU 架构)、一般保护错误,这意味着您的代码正在尝试读取或写入它不应该的内存。
Looking at your code, either room
is not set (maybe it is NULL still?) or it has been freed already, or door_x
or door_y
have ended up bigger than you allocated for room
(maybe your randomNumber
method has a bug?)
看你的代码,或者room
未设置(可能是空还是?),或者它已经被释放,或者door_x
或者door_y
已经结束了比你的更大的分配room
(也许你的randomNumber
方法有一个bug?)
You need to print the values for room
, door_x
, and door_y
before your code crashes, and you need to show us the rest of the code so we can see how you've allocated room
and how randomNumber
works (and how the size of the map is worked out).
您需要打印的值room
,door_x
和door_y
之前你的代码崩溃,你需要向我们展示了代码的其余部分,所以我们可以看到你分配room
,以及如何randomNumber
工作(以及地图的大小如何解决) .
And @zenith is right, it's lldb
, the LLVM debugger, not 11db
. You should read a tutorial on lldb
, because you can get huge amounts of information from that prompt.
@zenith 是对的,它是lldb
LLVM 调试器,而不是11db
. 您应该阅读关于 的教程lldb
,因为您可以从该提示中获得大量信息。