【问题标题】:sdl/opengl segfaults on first opengl call (macbook pro/macports)第一次调用 opengl 时出现 sdl/opengl 段错误(macbook pro/macports)
【发布时间】:2022-04-03 14:08:20
【问题描述】:

我过去(分别)使用过 SDL 和 glut 没有太大问题,现在我想在一个项目中使用 SDL/opengl。当我调用第一个 openGl 函数时出现段错误,无论我尝试哪个 openGl 函数。

我尝试过运行我在网上找到的 5 个不同的示例程序。我尝试了this SDLgears example from the sdl website,只对源进行了很小的更改(SDL_GetKeyState --> SDL_GetKeyboardState),但它也失败了。这让我觉得我的库可能有问题?

这是我相关的 macports 库:

libsdl-devel @1.3.0-5552_0 (active)
libsdl_mixer @1.2.11_3 (active)
mesa @7.8.2_2 (active)

虽然我不清楚 -lGL 是台面还是系统库。

我尝试过的所有示例都归结为:

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

void
warn_if_fail(int sdl_ret)
{
    if (sdl_ret < 0)
      printf("opengl error: %s\n", SDL_GetError());
}

int main(int argc __attribute__((unused)), char* args[] __attribute__((unused)))
{
    // init sdl video
    SDL_Init(SDL_INIT_VIDEO);

    //print video info
    const SDL_VideoInfo* info = SDL_GetVideoInfo();
    printf("video card memory: %d\n",         info->video_mem);
    printf("current_w: %d, current_hh: %d\n", info->current_w, info->current_h);
    printf("bpp: %d\n",                       info->vfmt->BitsPerPixel);
    printf("hardware_available: %d\n",        info->hw_available);
    printf("blit_hw: %d\n",                   info->blit_hw );

    // set SDL_GL attributes
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_RED_SIZE,      8)  );
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,     8)  );
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,    8)  );
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,    8)  );
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,  32)  );
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,  1)  );
    warn_if_fail(  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,   32)  );

    // set video mode
    SDL_Surface* screen;
    if ( (screen = SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL) ) == NULL)
        printf("video error: %s\n", SDL_GetError());
    printf("\nSDL_SetVideoMode success!\n");

    // print some attributes to make sure they were set correctly
    int red, green, blue, doublebuf;
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
    printf("red size, green size, blue size: <%d, %d, %d>\n", red, green, blue);
    printf("double buffered? %s\n\n", doublebuf == 1 ? "yes" : "no");

    // init openGl stuff
    printf("about to segfault\n");
    glViewport(0,0,640,480);
    printf("won't reach this message\n");

    // this also would have segfaulted
    // All openGl functions I've tried have segfaulted
    glClearColor(0, 0, 0, 0); 

    SDL_Quit();
    return 0;
}

生成文件:

PROJ = sdl_opengl_test
CXX=g++
Q = @

CPP_SRC = sdl_opengl_test.cpp
OBJ = $(CPP_SRC:.cpp=.o)

WARNINGFLAGS = -Wall -Wextra -Wshadow -Werror
INCLUDES = -I/opt/local/include
CPPFLAGS = $(WARNINGFLAGS) $(INCLUDES) -D_THREAD_SAFE

LDFLAGS  = -L/opt/local/lib -lSDL -lGL -lGLU

$(PROJ): $(OBJ)
    @echo LD $@
    $(Q)$(CXX) $(OBJ) $(LDFLAGS) -o $@

%.o : %.cpp
    @echo CXX $@
    $(Q)$(CXX) $(CPPFLAGS) -c $< -o $@

clean:
    rm -f $(PROJ)
    rm -f $(OBJ)

程序输出:

greg@pimptop ~/space/blaah (git)-[master] % ./sdl_opengl_test
video card memory: 0
current_w: 1680, current_hh: 1050
bpp: 32
hardware_available: 0
blit_hw: 0

SDL_SetVideoMode success!
red size, green size, blue size: <8, 8, 8>
double buffered? yes

about to segfault
[1]    37420 segmentation fault  ./sdl_opengl_test

gdb 输出:

(gdb) run
Starting program: /Users/greg/space/blaah/sdl_opengl_test 
Reading symbols for shared libraries .++++++............................. done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
video card memory: 0
current_w: 1680, current_hh: 1050
bpp: 32
hardware_available: 0
blit_hw: 0
Reading symbols for shared libraries . done
Reading symbols for shared libraries .. done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done

SDL_SetVideoMode success!
red size, green size, blue size: <8, 8, 8>
double buffered? yes

about to segfault

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000100000d55 in main (argc=1, args=0x7fff5fbff378) at sdl_opengl_test.cpp:50

谢谢

【问题讨论】:

  • 您使用 SDL 1.3 有什么原因吗?它现在非常不稳定。
  • SDL 1.3 是您在安装 macports libsdl-devel 时获得的。今天我卸载了它并安装了 1.2 的 libsdl +devel 但这并没有解决我的问题。

标签: opengl sdl


【解决方案1】:

如果您的代码在第一次 OpenGL 调用时出现段错误,100 次中有 99 次是因为您的 OpenGL 上下文设置不正确。

This 似乎是一个很好的教程,可以从 mac 上的 OpenGL/SDL 开始。但是,它用于代码块,看起来您正在使用 gcc(我认为..?)和 shell 而不是图形 IDE。您可以尝试复制粘贴 SDL/OpenGL 教程中的确切代码并查看它是否有效。如果没有,您知道这与您的标头/库有关。

强烈建议做的是消除所有不必要的 SDL 调用并运行您可能想出的最基本的程序。但是,您似乎已经完成了这项工作。

关于我上面提到的标题/库的主题,我发现这是最有可能的嫌疑人。我相信 SDL 的最新稳定版本是 1.2,我注意到您使用的是 1.3。你使用的是 1.3 的库和 1.2 的库吗?尝试将您提到的这三个库替换为最新的稳定版本。

祝你好运!如果我所说的没有任何帮助并且您仍然遇到问题,请务必发表评论。

【讨论】:

  • 哈!我没有注意到他试图将 1.2 库与 1.3 库一起使用。我刚刚看到了 1.3 SDL。
  • 如果你没有指出,我可能不会注意到。这是一个团队的努力;)
【解决方案2】:

这个问题帮助了我:https://github.com/libsdl-org/SDL/issues/4428

使用 SDL_CreateWindow api 代替 SDL_CreateWindowAndRenderer 以便在 mac 上创建 OpenGL 上下文。

【讨论】:

  • 不仅在 Mac 上。如果您要使用 GL,那么使用 SDL_Renderer 是个坏主意。
  • @HolyBlackCat 同意,它只是用于在我拥有的代码中工作,而不再这样做。
猜你喜欢
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
相关资源
最近更新 更多