LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2004, 01:25 PM   #1
fatherg
Member
 
Registered: Mar 2004
Posts: 55

Rep: Reputation: 15
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

Last edited by fatherg; 03-29-2004 at 01:29 PM.
 
Old 03-29-2004, 02:29 PM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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?
 
Old 03-29-2004, 03:07 PM   #3
fatherg
Member
 
Registered: Mar 2004
Posts: 55

Original Poster
Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segementation Fault Error (Core Dumped) ??? Mistro116@yahoo.com Programming 8 10-17-2005 11:10 AM
mod_perl causes segementation fault in Apache when php runs john lee Linux - Newbie 1 10-13-2005 12:44 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
Segementation fault on Multi threaded server on ia-64 Linux Latha Linux - Software 3 09-09-2004 06:40 PM
Calling my function through MACROS in OpenOfficeWriter mcp_achindra Linux - Software 0 07-05-2004 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration