Hey guys, happy Friday!
I'm working on an animation function using SDL, and I'm experiencing a bizarre problem I don't understand. Here are the details:
Code:
typedef struct {
int index;
unsigned char speed;
unsigned char counter;
SDL_Rect **tiles;
unsigned char totalTiles;
} Animation;
Animation CreateAnimation(SDL_Rect **tiles, unsigned char totalTiles, unsigned char speed) {
Animation a;
a.index = 0;
a.counter = 0;
a.speed = speed;
a.tiles = tiles;
a.totalTiles = totalTiles;
return a;
}
void DrawAnimation(SDL_Surface *image, Animation *a, int x, int y) {
a->counter++;
SDL_Rect *tile; // would be used later, struggling with debuggling. :P lol
for (unsigned char i = 0; i<a->totalTiles; i++) {
if (a->counter == a->speed * i) {
if (i > 0) a->index++;
a->counter = 0;
if (a->index == a->totalTiles) a->index = 0;
tile = a->tiles[i];
if (tile == NULL) printf("BINGO on %d\n", i);
break;
}
}
}
I've confirmed that:
1. a isn't NULL
2. a->index isn't NULL and is always less than a->totalTiles
3. a->tiles isn't NULL
4. nothing afaik in a is NULL
And yet, when I try to do this:
a->tiles[a->index]
The program crashes with code 0xC0000000005 (I may be off a zero or 60). I've confirmed this by checking if it's NULL, and it is. And yet, mysteriously, if I do this:
a->tiles[3] // The max value of a->index
It's fine. while(true) hair--; // :P
I've fought hard against 0xC0000000000000005 many times over, and it's one of those lovely ultra-vague goobers. Its official title is "access violation" and its real meaning is "You goofed with a pointer" (lol). But based on the info above, this scenario defies any kind of deductive reasoning or other human logic. Whatever it is that the computer thinks went NULL, it's obviously correct, but to us humans it's a real hair-jerker of a mystery. Something obviously broke, but I have no idea what/where/how/why. Any thoughts?
Thanks in advance.