Seg Fault in SDL_main()

This topic is 2662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm having some trouble with SDL. For whatever reason, my code will compile, but when run, it seg faults almost immediately. According to gdb, the seg fault occurs in SDL_main().
The first call I have in my main() function is to printf, and not even that gets executed.
I'm using mingw gcc to compile, and I'm linking with -lmingw32, -lSDLmain, -lSDL, and -lSDL_image if that helps at all.\

Thanks in advance for any help! =]

My main function:
int main(int argc, char *argv[]){ printf("1"); //variables unsigned int i; //for loops unsigned int j; //sets sprite sheets up for( i = 0; i <3; i++){ for (j = 0; j<3; j++){ pieceRect[i+j]->x = 200*j; pieceRect[i+j]->y = 200*i; pieceRect[i+j]->w = 200; pieceRect[i+j]->h = 200; } } //sets up locations for(i = 0; i<3; i++){ for(j = 0; j<3; j++){ locations[j] = i+j; } } //initialize SDL if(init() == -1){ return -1; } printf("4"); //Load image image == loadImage("bird.jpg"); if (image == NULL){ return -1; } printf("5"); //main loop while(running == 1){ while (SDL_PollEvent(&event)){ parseEvent(&event); } //blitBoard(); SDL_Flip(screen); } //clean up, clean up, everybody do their share SDL_FreeSurface(image); SDL_FreeSurface(screen); SDL_Quit(); return 0; }

Share on other sites
Do you have any globals? In particular global classes? Something with a constructor?

Share on other sites

Do you have any globals? In particular global classes? Something with a constructor?

I do have some globals, they're in the code block below. However, no classes.

//globals SDL_Surface* screen = NULL; // base surface int running = 0; SDL_Rect* pieceRect[9]; //sprite sheet location int locations[3][3]; //mapping of the pieces SDL_Surface* image; SDL_Event event;

Share on other sites

[quote name='nobodynews' timestamp='1299718410' post='4783804']
Do you have any globals? In particular global classes? Something with a constructor?

I do have some globals, they're in the code block below. However, no classes.

//globals SDL_Surface* screen = NULL; // base surface int running = 0; SDL_Rect* pieceRect[9]; //sprite sheet location int locations[3][3]; //mapping of the pieces SDL_Surface* image; SDL_Event event;
[/quote]

pieceRect is an array of uninitialised pointers. That would be the first thing I'd check.

Also, your first nested for loop under the comment "//sets sprite sheets up" looks dodgey. The indices [i+j] look like it might not be what you are intending there - but that's just a guess.

Share on other sites

pieceRect is an array of uninitialised pointers. That would be the first thing I'd check.

Ah! that was it! Thank you veryy much!

Also, your first nested for loop under the comment "//sets sprite sheets up" looks dodgey. The indices [i+j] look like it might not be what you are intending there - but that's just a guess.
[/quote]

I'm fairly certain that's what I want. It has to do with how my sprites are set up in the file. But thanks for checking anyways =]

Share on other sites
What version of SDL are you using?

If you haven't solved the problem yet, I'd recommend creating a minimal complete example that exhibits the behavior, and then post that (just put it all in a single source file). Be sure to eliminate anything that's not directly relevant and/or doesn't have any affect on the behavior. By doing that, you should either find the cause of the problem, or end up with a minimal, clear example that'll be easy for us to take a look at.

[Edit: Problem solved, apparently, so nm :) Still, consider the above advice for the future, as the process described above would likely have helped you to identify the cause of the problem.]

Share on other sites

I'm fairly certain that's what I want. It has to do with how my sprites are set up in the file. But thanks for checking anyways =]

Ok if you're sure

But let me just point out that your nested for loop will only assign your array up to 4 (ie when i == 2 and j ==2). The reason I think this isn't what you intend is because you made the array size 9.

Good luck.

Share on other sites

What version of SDL are you using?

If you haven't solved the problem yet, I'd recommend creating a minimal complete example that exhibits the behavior, and then post that (just put it all in a single source file). Be sure to eliminate anything that's not directly relevant and/or doesn't have any affect on the behavior. By doing that, you should either find the cause of the problem, or end up with a minimal, clear example that'll be easy for us to take a look at.

[Edit: Problem solved, apparently, so nm Still, consider the above advice for the future, as the process described above would likely have helped you to identify the cause of the problem.]

Thank you for this idea. I'll definitely use this as a method to debug in the future! =]

Ok if you're sure

But let me just point out that your nested for loop will only assign your array up to 4 (ie when i == 2 and j ==2). The reason I think this isn't what you intend is because you made the array size 9.

Good luck.

Ahh you win! In my defense I wrote that around 2 this morning. But now I can't think of a clever solution other than making three loops. Ah well, I learn either way.

Edit: Nevermind, I figure [j + (i*3)] is what I needed. =]

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 12
• 9
• 12
• 37
• 12
• Forum Statistics

• Total Topics
631415
• Total Posts
2999964
×