tags:

views:

524

answers:

2

OK - I have an interesting one here. I'm working on a tetris clone (basically to "level-up" my skills). I was trying to refactor my code to get it abstracted the way I wanted it. While it was working just fine before, now I get a segmentation fault before any images can be blitted. I've tried debugging it to no avail.

I have posted my SVN working copy of the project here.

It's just a small project and someone with more knowledge than me and a good debugger will probably figure it out in a snap. The only dependency is SDL. Kudos to the person that can tell me what I'm doing wrong.

Edit: As far as I can tell, what I have now and what I had before are logically the same, so I wouldn't think that what I have now would cause a segmentation fault. Just run an svn revert on the working copy, recompile and you can see that it was working...

A: 

I don't have SDL installed on my machine, but after looking through the code.

I noticed this in the Output.cpp file:

display = new Surface();

You do nothing. The constructor for this is empty. (surface is not initialized).

Then in Output::initalize() you do:

display->surface = SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );

This looks like the issue Surface::surface was never actually initialized. If you haven't found the solution, when i get home i'll digg into it.

UberJumper
That should be fine, because SDL_SetVideoMode(...) will create and initialize the screen surface and then pass the pointer back to display->surface
Dan
+3  A: 

Look at line 15 to 18 of Surface.cpp:

 surface = SDL_DisplayFormatAlpha( tempSurface );
 surface = tempSurface;
}
SDL_FreeSurface( tempSurface );

I assume it segfaults because when you use this surface later, you are actually operating on tempSurface because of this line:

surface = tempSurface;

and not the surface returned by SDL_DisplayFormatAlpha(). Since you free tempSurface, surface is now pointing to invalid memory. To fix, simply remove the second line in the else block.

Dan
doh! I had a feeling it was something simple... The next question is: what in the world impressed me to do that? :P
Steven Oxley
Oh wait - i remember now - i was getting rid of SDL_DisplayFormatAlpha entirely to see if that was the problem. Then I forgot to get rid of that line when I put SDL_DisplayFormatAlpha back in.
Steven Oxley