Common Issue -- HELP!

Hey SDL folk,

Compiling a rudimentary SDL app from an online tutorial (code below).

Using SDL 1.2.5. Everything builds up just fine but when I run I get a
stderr.txt msg reading:

Fatal signal: Segmentation Fault (SDL Parachute Deployed)

When I run the app in debug mode it tells me I’m having an access violation.

I’m running W2K with DirectX 8.1 SDK installed. I’ve got the DLLs set to
multi-threaded, my libraries are linked, everything is building. When I run
the little app, which is supposed to draw colors on the page, the screen
goes blank to fullscreen mode, which is correct, and then pauses for moment
before quitting and stderring out the text file.

Bummer.

From trolling Google I see that this is not an uncommon error, but I can’t
find any consistent fix or cause. Help!!!

Code Listing************
#include <stdio.h>
#include <stdlib.h>

#include “SDL/SDL.h”

void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel) {
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 )screen->pixels + yscreen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x * 3;
if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 )screen->pixels + yscreen->pitch/4 + x;
*bufp = color;
}
break;
}
}

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
if ( SDL_LockSurface(screen) < 0 )
return;
}

void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
SDL_UnlockSurface(screen);
}

void DrawScene(SDL_Surface *screen)
{
Slock(screen);
for (int x = 0; x != 640; ++x)
for (int y = 0; y != 480; ++y)
DrawPixel(screen, x, y, y/2, y/2, x/3);

Sulock(screen);
SDL_Flip(screen);

}

int main(int argc, char *argv[])
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)
printf(“Unable to init SDL: %s\n”, SDL_GetError());

atexit(SDL_Quit);

SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 32,

SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
if (screen = NULL) {
printf(“Unable to set 640x480 video mode: %s\n”, SDL_GetError());
exit(1);
}

int nDone = 0;
while (nDone == 0) {
	SDL_Event event;
	while (SDL_PollEvent(&event)) {
		if (event.type == SDL_QUIT) nDone = 1;
		if (event.type == SDL_KEYDOWN)
			if (event.key.keysym.sym == SDLK_ESCAPE) nDone = 1;
	}

	DrawScene(screen);
}

return 0;

}

:: Greg McClure
:: Third Stone Media
:: @Greg_McClure
:: http://www.thirdstone.net
:: (949) 719-9678 ph
:: (978) 383-8306 fx

Outgoing mail has been checked for viruses by AVG before being sent.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 8/21/2002

When I run the app in debug mode it tells me I’m having an access
violation.

[…]

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )

It crashes here because “screen” is NULL.

if (screen = NULL) {

Which is not strange, because you are setting “screen” to NULL right
after you got it. You probably meant to say:

if (screen == NULL) {

This was very easy to find from the Visual C++ debugger, but you
have to pass the flag SDL_INIT_NOPARACHUTE to SDL_Init(), otherwise
the debugger won’t work. When your program crashes like this, it is
also wise to run it in windowed mode, i.e. pass 0 as the flags to
SDL_SetVideoMode, otherwise the debugger doesn’t do much either.
This, of course, assumes you are using Visual C++.–
Matthijs Hollemans
All Your Software
www.allyoursoftware.com

Thanks for the extra pair of eyes on the code, Matthijs, and for the info on
SDL_NO_PARACHUTE. Problem solved and I appreciate the help!

Peace,

:: Greg McClure
:: Third Stone Media
:: @Greg_McClure
:: http://www.thirdstone.net
:: (949) 719-9678 ph
:: (978) 383-8306 fx> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Matthijs Hollemans
Sent: Wednesday, October 09, 2002 3:25 AM
To: sdl at libsdl.org
Subject: Re: [SDL] Common Issue – HELP!

When I run the app in debug mode it tells me I’m having an access
violation.

[…]

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )

It crashes here because “screen” is NULL.

if (screen = NULL) {

Which is not strange, because you are setting “screen” to NULL right
after you got it. You probably meant to say:

if (screen == NULL) {

This was very easy to find from the Visual C++ debugger, but you
have to pass the flag SDL_INIT_NOPARACHUTE to SDL_Init(), otherwise
the debugger won’t work. When your program crashes like this, it is
also wise to run it in windowed mode, i.e. pass 0 as the flags to
SDL_SetVideoMode, otherwise the debugger doesn’t do much either.
This, of course, assumes you are using Visual C++.

Matthijs Hollemans
All Your Software
www.allyoursoftware.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 8/21/2002


Outgoing mail has been checked for viruses by AVG before being sent.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 8/21/2002