Sign in to follow this  

I got Fatal signal: Segmentation Fault (SDL Parachute Deployed) but don't know why!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got Fatal signal: Segmentation Fault (SDL Parachute Deployed) in stderr but don't know why! I did the SDL_INIT_NOPARACHUTE thing and the problem seems to happen when the program exits because it either freezes up there or it puts the error in stderr. Take a look.
/*
  Name: Pong
  Copyright: None what so ever.
  Author: Joe Bates
  Date: 
  Description: Testing my abilitites as a programmer.
*/

//Headers
#include <stdlib.h>

#include <SDL\SDL.h>
#include <SDL\SDL_gfx.h>
#include <SDL\SDL_image.h>

//Main
int main(int argc, char *argv[])
{
    //Initialize SDL and the screen.
    SDL_Init(SDL_INIT_VIDEO ); atexit(SDL_Quit);
    SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
    SDL_WM_SetCaption("Pong", NULL);
    
    //Initialize the framerate.
    FPSmanager *framerate;
    SDL_initFramerate(framerate);
    SDL_setFramerate(framerate, 100);
    
    //Initialize images.
    SDL_Surface *lpaddle = IMG_Load("L_Paddle.png");
    SDL_Surface *rpaddle = IMG_Load("R_Paddle.png");
    SDL_Surface *ballimg = IMG_Load(    "Ball.png");
    
    //Initialize variables.
    SDL_Rect lrect;
    lrect.x =   5; lrect.y = 200;
    
    SDL_Rect rrect;
    rrect.x = 615; rrect.y = 200;
    
    SDL_Rect ball ;
    ball.x  = 310; ball.y  = 230;
    float ballmx = 4;
    float ballmy = 0;
    
    int wait = 1;
    
    //Loop start.
    bool done = false;
    while(done == false)
    {
        SDL_Event event;
        
        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_QUIT )  {  done = true;  }
            if ( event.type == SDL_KEYDOWN )
            {
                if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = true; }
            }
        }
        
        //Clear the screen.
        SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,0,0,0));
        
        //Get keys.
        Uint8 *keys = SDL_GetKeyState(NULL);
        
        //Respond to keys.
        if     (keys[SDLK_LSHIFT]) lrect.y-=4;
        else if(keys[SDLK_LCTRL] ) lrect.y+=4;
        
        if     (keys[SDLK_UP]  ) rrect.y-=4;
        else if(keys[SDLK_DOWN]) rrect.y+=4;
        
        //Block paddles.
        if(lrect.y <   0) lrect.y =   0;
        if(lrect.y > 400) lrect.y = 400;
        
        if(rrect.y <   0) rrect.y =   0;
        if(rrect.y > 400) rrect.y = 400;      
        
        //Left paddle bounce detection.
        if
        (
            (ball.x <= 15)
            &&
            ((ball.y + 20) >= lrect.y)
            &&
            ( ball.y       <= (lrect.y + 80))
        )
        {
            ballmy = (((ball.y + 10) - (lrect.y + 40)) /10);
            if     (ballmy < 0) ballmx = 4 + ballmy;
            else if(ballmy > 0) ballmx = 4 - ballmy;
            else ballmx = 4;
            
        }
        
        //Right paddle bounce detection.
        if
        (
            (ball.x >= 605)
            &&
            ((ball.y +20) >=  rrect.y)
            &&
            ( ball.y      <=  (rrect.y + 80))
        )
        {
            ballmy = (((ball.y + 10) - (rrect.y + 40)) /10);
            if     (ballmy < 0) ballmx = -4 - ballmy;
            else if(ballmy > 0) ballmx = -4 + ballmy;
            else ballmx = -4;
            
        }
        
        //Wall bounce detection.
        if((ball.y >= 460) || (ball.y <= 0)) ballmy = -ballmy;
        
        //Score detection.
        if((ball.x <= 0) || (ball.x >= 620))
        {
            ball.x = 310;
            ball.y = 230;
            ballmx =   4;
            ballmy =   0;
            wait = 1;
        }
        
        //Move ball.
        if(wait == 0)
        {
            ball.x += ballmx;
            ball.y += ballmy;
        }
        
        //Ball regeneration wait.
        if(wait !=   0)wait++;
        if(wait == 150)wait = 0;
        
        //Draw
        if((wait > 50) || (wait == 0)) SDL_BlitSurface(ballimg, NULL, screen, &ball);
        SDL_BlitSurface(lpaddle, NULL, screen, &lrect);
        SDL_BlitSurface(rpaddle, NULL, screen, &rrect);
        
        rectangleRGBA(screen, 0, 0, 639, 479, 255, 255, 255, 255);
        
        //Synchronize the screen.
        SDL_Flip(screen);
        SDL_framerateDelay(framerate);
    }      
}    

The program runs fine when the parachute is there, but I feel bad seeing it. BTW, I made the SDL_gfx header so I wouldn't have to include individual parts of SDL_gfx, incase u thought that was the problem.

Share this post


Link to post
Share on other sites
so it only crash's when you try to exit the game? i only glanced at your code, and i don't know the problem. step through your code with a debugger, and it should bring you to the exact line that is crashing the game. a few things:

-your not free'ing any of your surfaces when the game is over. this is a memory leak your causing! the OS will clean it up for you, but this is just a bad programming practice

-god invented functions for a reason, man. [smile]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You need to allocate the FPSmanager:

FPSmanager *framerate = (FPSmanager *) malloc(sizeof(FPSmanager));

or alternatively, don't declare it a pointer:

FPSmanager framerate;
SDL_initFramerate(&framerate);
SDL_setFramerate(&framerate, 100);

Share this post


Link to post
Share on other sites
I used the exit(0) function and it worked fine, but the files still appear, just empty. Any way to have like it ot make those files?

I will make it with functiosn later.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Did you read my post above?

You have to allocate the FPSmanager. I compiled your code
and that was the error!

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this