【问题标题】:SIGSEGV error in c++ SDLC++ SDL 中的 SIGSEGV 错误
【发布时间】:2017-10-25 17:51:38
【问题描述】:

此代码导致 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 游戏开发”一书。但是,我使用的是 mingw,而不是书中建议的 Visual Studio。我已经按照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 函数中的问题,但我不知道出了什么问题。

【问题讨论】:

  • 在 gdb 中,您可以使用 print 命令来显示变量和表达式的值。你可能想看看这个:L ericlippert.com/2014/03/05/how-to-debug-small-programs
  • 当你处理 g_pWindow 不是 NULL 时,你不处理 g_pWindow IS NULL。我的怀疑是它正在返回NULL,并且这会延续到程序的其余部分。这可能是参数,权限之类的东西。
  • 使用backtrace(或bt)gdb 命令获取堆栈跟踪,将其输出添加到问题中。您使用哪个 SDL 版本?你确定你有正确的 mingw 的 lib/dll(而不是 MSVC)吗?你能创建软件渲染器(使用SDL_RENDERER_SOFTWARE)吗?如果不能,stacktrace 在这种情况下会有所不同吗?

标签: c++ mingw sdl segmentation-fault


【解决方案1】:

由于这条线,您遇到了分段错误 -

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

原因是g_pWindowNULL。正如您在这一行中使用 NULL 初始化它一样 -

SDL_Window* g_pWindow = 0;

您需要提供SDL_Window的有效指针。

详情请查看this.

【讨论】:

  • 不可能是这种情况,因为 if 语句阻止了此调用使用 nullptr。我想问题是我的机器上没有 gpu 驱动程序。我稍后会尝试安装它,然后再试一次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 2018-09-25
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多