I’m trying to follow Twinklebear’s SDL2 tutorial. Lesson 0 compiled and ran properly so I assume I set up SDL2 correctly. Now I’m trying to do Lesson 1 (http://www.willusher.io/sdl2%20tutorials/2013/08/17/lesson-1-hello-world/) and the program crashes with a segfault. I’ll provide gdb output later. It compiles without errors or warnings.
Now I got myself a gdb tutorial and tried to find the reason for the segfault. It seems that a call to SDL_CreateTextureFromSurface is responsible, but the two pointers given in the parameters are not nullptr. I don’t understand how I could proceed from here on.
Could somebody help me to proceed in finding the root cause? Thanks a lot! Please find attached the code and the steps I did with gdb (and the output).
The slightly changed tutorial code:
[code]
#include
#include <SDL2/SDL.h>
int main(int, char**){
//First we need to start up SDL, and make sure it went ok
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
//Make sure creating our window went ok
if (win == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return 1;
}
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
//video driver supports the flags we're passing
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
//synchornized with the monitor's refresh rate
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
if (ren == nullptr){
SDL_DestroyWindow(win);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
//this lets us choose when to upload or remove textures from the GPU
std::string imagePath = "hello.bmp";
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
if (bmp == nullptr){
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
//To use a hardware accelerated texture for rendering we can create one from
//the surface we loaded
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
//We no longer need the surface
SDL_FreeSurface(bmp);
if (tex == nullptr){
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
//A sleepy rendering loop, wait for 3 seconds and render and present the screen each time
for (int i = 0; i < 3; ++i){
//First clear the renderer
SDL_RenderClear(ren);
//Draw the texture
SDL_RenderCopy(ren, tex, NULL, NULL);
//Update the screen
SDL_RenderPresent(ren);
//Take a quick break after all that hard work
SDL_Delay(1000);
}
//Clean up our objects and quit
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
The gdb output:
(gdb) run
Starting program: /home/username/git/test123/bin/Debug/test123
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff78e6379 in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
(gdb) backtrace
#0 0x00007ffff78e6379 in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#1 0x00007ffff789438a in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#2 0x0000000000401364 in main () at /home/username/git/test123/main.cpp:46
(gdb) frame 2
#2 0x0000000000401364 in main () at /home/username/git/test123/main.cpp:46
46 SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
(gdb) print ren
$1 = (SDL_Renderer *) 0x6700b0
(gdb) print bmp
$2 = (SDL_Surface *) 0x777fd0