简体   繁体   English

C ++ SDL中的SIGSEGV错误

[英]SIGSEGV error in c++ SDL

This code is making SIGSEGV error and I couldn't puzzle out why. 这段代码使SIGSEGV错误,我不知道为什么。

#include<SDL.h>

SDL_Window* g_pWindow = 0;
SDL_Renderer* g_pRenderer = 0;
int main(int argc, char* args[])
{
    // initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
    {
       // if succeeded create our window
       g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
       SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,640, 480,SDL_WINDOW_SHOWN);
       // if the window creation succeeded create our renderer
       if(g_pWindow != 0)
       {
           g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
       }
    }
    else
    {
            return 1; // sdl could not initialize
    }
    // everything succeeded lets draw the window
    // set to black // This function expects Red, Green, Blue and
    // Alpha as color values
    SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
    // clear the window to black
    SDL_RenderClear(g_pRenderer);
    // show the window
    SDL_RenderPresent(g_pRenderer);
    // set a delay before quitting
    SDL_Delay(5000);
    // clean up SDL
    SDL_Quit();
    return 0;
}

This code is from "SDL game development" book by Sean Mitchell. 此代码来自Sean Mitchell撰写的“ SDL游戏开发”一书。 But instead of Visual Studio as suggested in the book I'm using mingw. 但是我没有使用本书中建议的Visual Studio,而是使用mingw。 I've configured everything as described in lazyfoo's tutorials: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/mingw/index.php Btw his "Hello SDL" works fine. 我已经按照lazyfoo的教程中的说明配置了所有内容: http ://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/mingw/index.php他的“ Hello SDL”工作正常。 Here is my Makefile: 这是我的Makefile:

#OBJS specifies which files to compile as part of the project
OBJS = main.cpp

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = hello

#This is the target that compiles our executable
all : $(OBJS)
    g++ $(OBJS) -IC:\Artur\Projects\SDL\include\SDL2 -LC:\Artur\Projects\SDL\lib -w  -lmingw32 -lSDL2main -lSDL2 -o $(OBJ_NAME) -g

And what I've found with gdb: 我在gdb中发现的东西:

(gdb) run
Starting program: C:\Artur\Projects\CPP\Snake/hello.exe
[New Thread 4800.0x45c]
[New Thread 4800.0xc7c]
[New Thread 4800.0xc48]
[New Thread 4800.0xa8c]
[New Thread 4800.0xbc0]
[New Thread 4800.0x1350]

Breakpoint 1, SDL_main (argc=argc@entry=1, args=args@entry=0x3b0008)
    at main.cpp:17
17      g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
(gdb) s
[New Thread 4800.0xf98]

Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? <>

So I know that the problem in SDL_CreateRenderer function but I don't know what's wrong. 所以我知道SDL_CreateRenderer函数中的问题,但我不知道出了什么问题。

You are getting Segmentation fault because of this line - 由于这一行,您遇到了细分错误-

g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);

The reason is g_pWindow is NULL . 原因是g_pWindowNULL As you have initialized it with NULL in this line - 当您在这一行中使用NULL进行初始化时-

SDL_Window* g_pWindow = 0;

You need to provide valid pointer of SDL_Window . 您需要提供SDL_Window有效指针。

For details, please check this. 有关详细信息,请检查此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM