I’m using Debian Testing and these are the installed packages:
|libsdl2-2.0-0:amd64||2.0.10+dfsg1-3|
|---|---|---|
|libsdl2-2.0-0:i386||2.0.10+dfsg1-3|
|libsdl2-dev:amd64||2.0.10+dfsg1-3|
|libsdl2-gfx-1.0-0:amd64||1.0.4+dfsg-3+b1|
|libsdl2-gfx-dev:amd64||1.0.4+dfsg-3+b1|
|libsdl2-image-2.0-0:amd64||2.0.5+dfsg1-2|
|libsdl2-image-dev:amd64||2.0.5+dfsg1-2|
|libsdl2-mixer-2.0-0:amd64||2.0.4+dfsg1-2+b1|
|libsdl2-mixer-dev:amd64||2.0.4+dfsg1-2+b1|
|libsdl2-net-2.0-0:amd64||2.0.1+dfsg1-4+b1|
|libsdl2-net-dev:amd64||2.0.1+dfsg1-4+b1|
|libsdl2-ttf-2.0-0:amd64||2.0.15+dfsg1-1|
|libsdl2-ttf-dev:amd64||2.0.15+dfsg1-1|
Trying to compile and run a small Hello World like code (from lazyfoo’s)
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
void init();
void loadMedia();
void close();
SDL_Window * gWindow = NULL;
SDL_Surface * gScreenSurface = NULL;
SDL_Surface * gHelloWorld = NULL;
void
init_interface() {
SDL_Init(SDL_INIT_VIDEO);
gWindow = SDL_CreateWindow("Fractal",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
gScreenSurface = SDL_GetWindowSurface(gWindow);
}
void loadMedia()
{
gHelloWorld = SDL_LoadBMP("hello.bmp");
}
void close()
{
SDL_FreeSurface(gHelloWorld);
SDL_DestroyWindow(gWindow);
SDL_Quit();
}
int main(int argc, char *argv[]) {
init_interface();
loadMedia();
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(gWindow);
SDL_Delay(20000);
close();
return 0;
}
Returns the error:
free(): double free detected in tcache 2
Aborted
Changing the code to a simpler version (not using globals or function calls)
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int main(int argc, char *argv[]) {
SDL_Window * gWindow = NULL;
SDL_Surface * gScreenSurface = NULL;
SDL_Surface * gHelloWorld = NULL;
SDL_Init(SDL_INIT_VIDEO);
gWindow = SDL_CreateWindow("Fractal",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
gScreenSurface = SDL_GetWindowSurface(gWindow);
gHelloWorld = SDL_LoadBMP("hello.bmp");
SDL_BlitSurface(gHelloWorld, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(gWindow);
SDL_Delay(2000);
SDL_FreeSurface(gHelloWorld);
SDL_DestroyWindow(gWindow);
SDL_Quit();
return 0;
}
Works fine. What is wrong with original code?