繁体   English   中英

C ++ SDL中的SIGSEGV错误

[英]SIGSEGV error in c++ SDL

这段代码使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;
}

此代码来自Sean Mitchell撰写的“ SDL游戏开发”一书。 但是我没有使用本书中建议的Visual Studio,而是使用mingw。 我已经按照lazyfoo的教程中的说明配置了所有内容: http ://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/mingw/index.php他的“ Hello SDL”工作正常。 这是我的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

我在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 ?? <>

所以我知道SDL_CreateRenderer函数中的问题,但我不知道出了什么问题。

由于这一行,您遇到了细分错误-

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

原因是g_pWindowNULL 当您在这一行中使用NULL进行初始化时-

SDL_Window* g_pWindow = 0;

您需要提供SDL_Window有效指针。

有关详细信息,请检查此。

暂无
暂无

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

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