LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SDL Segementation Fault when calling function SDL_LockSurface (https://www.linuxquestions.org/questions/programming-9/sdl-segementation-fault-when-calling-function-sdl_locksurface-163824/)

fatherg 03-29-2004 01:25 PM

SDL Segementation Fault when calling function SDL_LockSurface
 
Hi, i'm using gcc on redhat 9, i just installed my SDL RPM's(src and bin's). I'm not sure if the problem is with my install of SDL, or if my code is incorrect. Here's my code:
[PRE]
//main.c
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>

Uint16 CreateHicolorPixel(SDL_PixelFormat * fmt, Uint8 red, Uint8 green, Uint8 blue);

int main()
{
SDL_Surface *screen;
Uint16 *raw_pixels;
int x, y;


//init SDL's video system and check for errors
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}

//when program terminates make sure SDL_Quit is called
atexit(SDL_Quit);

screen = SDL_SetVideoMode(256, 256, 16, 0);

if(screen = NULL)
{
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}

printf("Locking Surface\n");
//we'll be accessing video memory so we had better lock it
if(!SDL_LockSurface(screen))//SEGMENT FAULT!!!
{
printf("Unable to Lock Surface\n");
return 1;
}
printf("Surface Locked\n");
//point to video surfaces memory
printf("Loading Pixels Into Memory\n");
raw_pixels = (Uint16 *) screen->pixels;
printf ("Pixels Loaded\n");

//ready to r0x0r
//draw gradient
for(x = 0; x < 256; x++)
{
for(y = 0; y < 256; y++)
{
Uint16 pixel_color;
int offset;

//generate color
pixel_color = CreateHicolorPixel(screen->format, x, 0, y);
//set offset addr to plot pixel
offset = (screen->pitch / 2 * y + x);
//do it to it
raw_pixels[offset] = pixel_color;
}
}

//unlock video memory
SDL_UnlockSurface(screen);

//inform SDL that screen has been changed
SDL_UpdateRect(screen, 0, 0, 0, 0);

//pause to view our masterpiece
SDL_Delay(3000);

return 0;
}

Uint16 CreateHicolorPixel(SDL_PixelFormat * fmt, Uint8 red, Uint8 green, Uint8 blue)
{
Uint16 value;

//use SDL_PixelFormat struct to compose 16-bit value from 8-bit rgb data
value = ((red >> fmt->Rloss) << fmt->Rshift) +
((green >> fmt->Gloss) << fmt->Gshift) +
((blue >> fmt->Bloss) << fmt->Bshift);

return value;
}
[/PRE]

when i execute my compiled code, here is my output:

$ ./direct_pixel
Locking Surface
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

thx for any help you can give me!
fatherg

jinksys 03-29-2004 02:29 PM

Code:

if(screen = NULL)
{
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}

printf("Locking Surface\n");
//we'll be accessing video memory so we had better lock it
if(!SDL_LockSurface(screen))//SEGMENT FAULT!!!
{
printf("Unable to Lock Surface\n");
return 1;
}

Your problem is probebly here. if(screen = NULL) sets screen to NULL, it doesnt test screen against null. it should read if(screen == NULL). Since you set screen to NULL, the if statement isnt true and the program doesnt exit. Then, SDL_LockSurface() gets a NULL address from screen, so it segfaults.

Understand?

fatherg 03-29-2004 03:07 PM

yes i do, thanks so much i can't believe i overlooked that... then again it is usually the simple stuff that gets us into trouble when it comes to programming...

mucho thx,
fatherg


All times are GMT -5. The time now is 03:50 AM.