admin管理员组

文章数量:1125618

I keep getting segfault errors and I'm not sure if I'm missing something.

main.c:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>

#include "../include/globals.h"
#include "../include/sprite.h"
#include "../include/subsys.h"

SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Surface *gHelloWorld = NULL;

int main(int argc, char* args[]){
    //Start up SDL and create window
    if(!init()){
        printf("Failed to initialize!\n");
    }
    else{
        //Load media
        if(!(create_sprite())){
            printf("Sprite initialization failed!\n");
        }
        else{
            //Apply the image
            SDL_Event e; 
            bool quit = false; 
            while(quit == false){ 
                while( SDL_PollEvent(&e)){ 
                    if(e.type == SDL_QUIT){ 
                        quit = true; 
                    }
                    else if (e.type == SDL_KEYDOWN){
                        switch (e.key.keysym.sym){
                            case SDLK_DOWN:
                                sprite_list[0].rect.y += 50;
                            case SDLK_UP:
                                sprite_list[0].rect.y -= 50;
                            case SDLK_LEFT:
                                sprite_list[0].rect.x -= 50;
                            case SDLK_RIGHT:
                                sprite_list[0].rect.x += 50;
                            case SDLK_ESCAPE:
                                quit = true;
                                break;
                        }
                    }
                } //main game loop
                SDL_RenderCopy(gRenderer, sprite_list[0].texture, NULL, &sprite_list[0].rect);
                SDL_RenderPresent(gRenderer);
                SDL_RenderClear(gRenderer);
            }
        }
    }
    close();
    return 0;
}

subsys.c:

#include <SDL2/SDL.h>
#include <stdbool.h>

#include "../include/globals.h"
#include "../include/subsys.h"
#include "../include/sprite.h"

bool init(){
    //Initialization flag
    bool success = true;
    //Initialize SDL
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    }
    else{
        //Create window
        gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
        SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if(gWindow == NULL){
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        }
        else{
            //Get window surface
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
        }
    }

    return success;
}

void close(){
    //Destroy window
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;
    //clear dynamic arrays from heap
    free(sprite_list);
    //Quit SDL subsystems
    SDL_Quit();
}

sprite.c:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>

#include "../include/sprite.h"
#include "../include/globals.h"
#include "../include/subsys.h"

int sprite_list_size = 0;
Sprite *sprite_list = NULL;

//TODO: You may have to make different lists for enemies vs. allies.
bool create_sprite(){
    //sprite list grows by 1 each time it's initialized
    sprite_list_size += 1;
    //Loading success flag
    bool success = true;
    //dynamic array grows first before more data is added to it
    if(sprite_list_size == 1){
        sprite_list = (Sprite*)malloc(sizeof(Sprite));
        if(!(sprite_list)){
            printf("ERROR: Failed to initialize sprite list in heap!!");
            success = false;
            return success;
        }
    }
    else{
        if(!(realloc(sprite_list, sizeof(Sprite) * sprite_list_size))){
            printf("ERROR: Failed to reallocate sprite list in heap!!");
            success = false;
            return success;
        }
    }
    //Load splash image
    if (!(gHelloWorld = IMG_Load("../resources/cute_doggo.jpeg"))){
        printf("Unable to load image %s! SDL Error: %s\n", "../resources/cute_doggo.jpeg", SDL_GetError());
        success = false;
        return success;
    }
    //take transfer surface to texture
    Sprite sprite = {{X_CENTER - 100 / 2, Y_CENTER - 100 / 2, 100, 100}, NULL};
    sprite.texture = SDL_CreateTextureFromSurface(gRenderer, gHelloWorld);
    //free surface
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;
    //make sure to add sprite to sprite array   
    sprite_list[sprite_list_size - 1] = sprite;
    //Only reallocate after the first initialization!
    return success;
}

globals.h:

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

//define center since SDL2 doesn't have a function for fetching the center coordinates 
#define X_CENTER SCREEN_WIDTH / 2
#define Y_CENTER SCREEN_HEIGHT / 2

subsys.h:

bool init();
void close();

extern SDL_Window *gWindow;
extern SDL_Surface *gHelloWorld;
extern SDL_Renderer *gRenderer;

sprite.h:

#ifndef MYSTRUCTS_H
#define MYSTRUCTS_H
typedef struct {
    SDL_Texture *texture;
    SDL_Rect rect;
}Sprite;
#endif

extern Sprite *sprite_list;
extern int sprite_list_size;

void move_sprite();
bool create_sprite();

I tried using GDB to see if there was anything obvious, but the only error message I got was ./libio/genops.c: No such file or directory.

I keep getting segfault errors and I'm not sure if I'm missing something.

main.c:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>

#include "../include/globals.h"
#include "../include/sprite.h"
#include "../include/subsys.h"

SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Surface *gHelloWorld = NULL;

int main(int argc, char* args[]){
    //Start up SDL and create window
    if(!init()){
        printf("Failed to initialize!\n");
    }
    else{
        //Load media
        if(!(create_sprite())){
            printf("Sprite initialization failed!\n");
        }
        else{
            //Apply the image
            SDL_Event e; 
            bool quit = false; 
            while(quit == false){ 
                while( SDL_PollEvent(&e)){ 
                    if(e.type == SDL_QUIT){ 
                        quit = true; 
                    }
                    else if (e.type == SDL_KEYDOWN){
                        switch (e.key.keysym.sym){
                            case SDLK_DOWN:
                                sprite_list[0].rect.y += 50;
                            case SDLK_UP:
                                sprite_list[0].rect.y -= 50;
                            case SDLK_LEFT:
                                sprite_list[0].rect.x -= 50;
                            case SDLK_RIGHT:
                                sprite_list[0].rect.x += 50;
                            case SDLK_ESCAPE:
                                quit = true;
                                break;
                        }
                    }
                } //main game loop
                SDL_RenderCopy(gRenderer, sprite_list[0].texture, NULL, &sprite_list[0].rect);
                SDL_RenderPresent(gRenderer);
                SDL_RenderClear(gRenderer);
            }
        }
    }
    close();
    return 0;
}

subsys.c:

#include <SDL2/SDL.h>
#include <stdbool.h>

#include "../include/globals.h"
#include "../include/subsys.h"
#include "../include/sprite.h"

bool init(){
    //Initialization flag
    bool success = true;
    //Initialize SDL
    if(SDL_Init(SDL_INIT_VIDEO) < 0){
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    }
    else{
        //Create window
        gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
        SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if(gWindow == NULL){
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
            success = false;
        }
        else{
            //Get window surface
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
        }
    }

    return success;
}

void close(){
    //Destroy window
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;
    //clear dynamic arrays from heap
    free(sprite_list);
    //Quit SDL subsystems
    SDL_Quit();
}

sprite.c:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>

#include "../include/sprite.h"
#include "../include/globals.h"
#include "../include/subsys.h"

int sprite_list_size = 0;
Sprite *sprite_list = NULL;

//TODO: You may have to make different lists for enemies vs. allies.
bool create_sprite(){
    //sprite list grows by 1 each time it's initialized
    sprite_list_size += 1;
    //Loading success flag
    bool success = true;
    //dynamic array grows first before more data is added to it
    if(sprite_list_size == 1){
        sprite_list = (Sprite*)malloc(sizeof(Sprite));
        if(!(sprite_list)){
            printf("ERROR: Failed to initialize sprite list in heap!!");
            success = false;
            return success;
        }
    }
    else{
        if(!(realloc(sprite_list, sizeof(Sprite) * sprite_list_size))){
            printf("ERROR: Failed to reallocate sprite list in heap!!");
            success = false;
            return success;
        }
    }
    //Load splash image
    if (!(gHelloWorld = IMG_Load("../resources/cute_doggo.jpeg"))){
        printf("Unable to load image %s! SDL Error: %s\n", "../resources/cute_doggo.jpeg", SDL_GetError());
        success = false;
        return success;
    }
    //take transfer surface to texture
    Sprite sprite = {{X_CENTER - 100 / 2, Y_CENTER - 100 / 2, 100, 100}, NULL};
    sprite.texture = SDL_CreateTextureFromSurface(gRenderer, gHelloWorld);
    //free surface
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;
    //make sure to add sprite to sprite array   
    sprite_list[sprite_list_size - 1] = sprite;
    //Only reallocate after the first initialization!
    return success;
}

globals.h:

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

//define center since SDL2 doesn't have a function for fetching the center coordinates 
#define X_CENTER SCREEN_WIDTH / 2
#define Y_CENTER SCREEN_HEIGHT / 2

subsys.h:

bool init();
void close();

extern SDL_Window *gWindow;
extern SDL_Surface *gHelloWorld;
extern SDL_Renderer *gRenderer;

sprite.h:

#ifndef MYSTRUCTS_H
#define MYSTRUCTS_H
typedef struct {
    SDL_Texture *texture;
    SDL_Rect rect;
}Sprite;
#endif

extern Sprite *sprite_list;
extern int sprite_list_size;

void move_sprite();
bool create_sprite();

I tried using GDB to see if there was anything obvious, but the only error message I got was ./libio/genops.c: No such file or directory.

Share Improve this question edited 2 days ago genpfault 52.1k12 gold badges91 silver badges147 bronze badges asked Jan 9 at 6:58 James MarshallJames Marshall 291 silver badge3 bronze badges New contributor James Marshall is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3
  • 1 Use a debugger to catch the crash and see when and where in your code it happens. Check values of all involved variables. Make sure there's no null pointers or uninitialized pointers. – Some programmer dude Commented Jan 9 at 7:15
  • 2 A few general tips to make your code easier to debug and fix: Don't do everything at once. Start with an empty main function. Build with extra warnings enabled, treated as errors. When it build cleanly, test the program in all ways you can think about. Once all tests passes, then add a very small bit of code, perhaps just one or two lines. Build and test as before. When there's a problem, it's much easier to isolate it, test it and debug it. – Some programmer dude Commented 2 days ago
  • Okay, I'll keep that in mind! Thanks for the tips. Most of this was hacked together from some code I found on the internet if it wasn't obvious. Not a smart thing to do if you're relatively new to writing code in C. I'm also still getting used to the compiler and gdb so next time I'll read more of the documentation before posting a question. – James Marshall Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 2

The close() symbol in subsys.c conflicts with the libc symbol close. https://linux.die.net/man/2/close

You might want to rename it to app_close or whatever prefix you want.

本文标签: SDL2 Segmentation Fault libiogenopsc No such file or directoryStack Overflow