I’m trying to write a simple card game using SDL. At the end of the
game I free the surfaces used for the cards like this:
void remCards(){
int i;
for(i = 0; i < 52; i++){
if(deck[i].img != NULL){ // Just a safety check
SDL_FreeSurface(deck[i].img);
}
}
}
The program crashes (SDL_Parachute) every time this method is called.
The crashes happen for a different value of i each time also. I’m
assuming this means that I’m trying to free memory that I shouldn’t be?
The only place that I think I might be messing this up is when I shuffle
the deck. The card struct looks like this:
struct card {
int value;
SDL_Surface *img;
int suit;
};
To shuffle the deck I have an (global) array of cards in order:
struct card deck[52];
I then create another array:
struct card shuffled[52];
I then randomly move cards from the ordered deck to the shuffled deck
like this:
shuffled[j].suit = deck[i].suit;
shuffled[j].value = deck[i].value;
shuffled[j].img = deck[i].img;
I then move the cards back to the original global deck.
Is there a problem with they way I’m assigning the images? If not, what
else could cause the Seg Fault?
Thanks,
Buck