我试图编写一个简单的SDL2和OpenGL程序,但SDL_Init
导致我的应用程序在开始时崩溃。
这是代码:
#include <iostream>
#include <cstdint>
#include <windows.h>
#include <GL/gl.h>
#include <SDL2/SDL.h>
#define WIN_WIDTH 800
#define WIN_HEIGHT 600
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) {
int err;
SDL_Window *window;
SDL_GLContext context;
uint32_t win_flags = SDL_WINDOW_OPENGL;
std::cout << "before";
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "fail";
std::cout << "SDL Init Error: " << SDL_GetError() << std::endl;
}
std::cout << "success";
window = SDL_CreateWindow("rgb", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WIN_WIDTH, WIN_HEIGHT, win_flags);
context = SDL_GL_CreateContext(window);
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
输出是:
before
然后它崩溃了。
调试器输出(当它进入WinMain时):
WinMain (hInstance=0x0, hPrevInstance=0x1d04aa0, lpCmdLine=0x0, nCmdShow=0)
at src/main.cpp:15
15 int nCmdShow) {
(gdb) s
20 uint32_t win_flags = SDL_WINDOW_OPENGL;
(gdb) s
22 std::cout << "before" << std::endl;
(gdb) s
before
24 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()