ObjC+Cocos2d:仅在设备上奇怪的 EXEC_BAD_ACCESS
我仅在设备上遇到奇怪的 EXEC_BAD_ACCESS 错误,它在模拟器中有效。
这是相关代码的一段:
#define MAX_ELEMENT_A 24
#define MAX_ELEMENT_B 24
#define MAX_ELEMENT_C 24
typedef struct {
int pos;
int index;
} A_STRUCTURE;
typedef struct {
int pos;
int index;
} B_STRUCTURE;
typedef struct {
int pos;
int index;
} C_STRUCTURE;
typedef struct {
A_STRUCTURE As[MAX_ELEMENT_A];
int a_count;
B_STRUCTURE Bs[MAX_ELEMENT_B];
int b_count;
C_STRUCTURE Cs[MAX_ELEMENT_C];
int c_count;
} LEVEL;
该结构基本上是一个游戏关卡,关卡元素从地图文件中读取:
LEVEL level;
int a_count;
int b_count;
int c_count;
for (int i = 0; i < map_size; i++) {
MAP_TILE tile = [map nextTile];
if (tile.type == TYPE_A && a_count < MAX_ELEMENT_A) {
level.As[a_count++] = tile.data;
} else {
// do the same for element B and C
}
}
level.a_count = a_count;
level.b_count = b_count;
level.c_count = c_count;
return level;
每次读取地图图块时,我都会检查其类型(A,B或C)并将它们放入相对数组中;我还检查特定元素类型的数量是否超过其最大值(数组大小)。
问题是,使用上述 MAX_ELEMENT_* 值(即全部 24),在模拟器或设备中运行没有问题,但如果我将其中任何两个从 24 更改为 64,如果仅在设备上运行,我会得到 EXEC_BAD_ACCESS。我试图找到它发生的地方,但没有成功,读取一个关卡后,它的数据然后传递到一个Box2d世界,错误发生在世界开始滴答之前,我猜这可能是由于64太大的一些内存问题引起的24 很好,但我对此表示怀疑,因为我使用 sizeof(LEVEL) 检查并得到 64 的 2*** ,这不应该太大,一次只能读取一个级别。
有人知道吗?
I got a weird EXEC_BAD_ACCESS error on device only, it works in emulator.
Here is the piece of related code:
#define MAX_ELEMENT_A 24
#define MAX_ELEMENT_B 24
#define MAX_ELEMENT_C 24
typedef struct {
int pos;
int index;
} A_STRUCTURE;
typedef struct {
int pos;
int index;
} B_STRUCTURE;
typedef struct {
int pos;
int index;
} C_STRUCTURE;
typedef struct {
A_STRUCTURE As[MAX_ELEMENT_A];
int a_count;
B_STRUCTURE Bs[MAX_ELEMENT_B];
int b_count;
C_STRUCTURE Cs[MAX_ELEMENT_C];
int c_count;
} LEVEL;
The structure is basically a game level, level elements are read from a map file:
LEVEL level;
int a_count;
int b_count;
int c_count;
for (int i = 0; i < map_size; i++) {
MAP_TILE tile = [map nextTile];
if (tile.type == TYPE_A && a_count < MAX_ELEMENT_A) {
level.As[a_count++] = tile.data;
} else {
// do the same for element B and C
}
}
level.a_count = a_count;
level.b_count = b_count;
level.c_count = c_count;
return level;
every time a map tile is read, I check its type(A, B or C) and put them into relative array; I also check if the number of a particular element type exceeded its maximum(array size).
The problem is, with the above MAX_ELEMENT_* value(ie. all 24), there is no problem running in simulator or device, but if I change any two of them from 24 to 64, I got EXEC_BAD_ACCESS if run on device ONLY. I tried to find where it occurred but no success, after reading a level, its data is then passed into a Box2d world, the error occurred before the world start ticking, I guess it may be caused by some memory issue that 64 is too large and 24 is fine, but I doubt it as I checked using sizeof(LEVEL) and got 2*** for 64 which should not be too large, only one level is read at a time.
Does anyone have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...看来您正在使用未初始化的局部变量。
Hmm... It seems that you are using uninitialized local variables.