ObjC+Cocos2d: Weird EXEC_BAD_ACCESS on device ONLY
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 = 开发者_StackOverflow社区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?
Hmm... It seems that you are using uninitialized local variables.
int a_count = 0;
int b_count = 0;
int c_count = 0;
精彩评论