carmraspberry-pi3sdl-2sdl-image

SDL2: IMG_Load() - Segmentation fault on Raspberry Pi


This simple code that perfectly works on Ubuntu crashes with a segmentation fault on my Raspberry Pi v3:

#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"

int main()
{
    char* artworkPath = "./testfile.png";

    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
    {
        SDL_Log("SDL fails to initialize video subsystem!\n%s", SDL_GetError());
        return -1;
    }
    else
        printf("SDL correctly initialized!\n");

    if((IMG_Init(IMG_INIT_PNG)&IMG_INIT_PNG) != IMG_INIT_PNG)
        printf("IMG_Init: %s\n", IMG_GetError());
    else
        printf("Fine!\n");

    SDL_Surface* artworkSurface = IMG_Load(artworkPath);

    if (artworkSurface == NULL)
    {
        printf("Error on loading PNG image\n");
        return -1;
    }
    else
        return 0;
}

Error on loading PNG image is never shown because it crashes during loading with a segmentation fault, any idea?


Solution

  • SOLUTION

    There are bugs on libpng 1.2.50 and 1.6.36 that prevent to correctly load the image.

    SDL Image library installs its own dependencies of such library, the correct/only way to use SDL Image on Raspberry Pi is to compile libpng and SDL library from source.

    So, download libpng16 (from GitHub) and SDL2_image-2.0.5 (from official site) source code, compile (libpng as first) and install them. In this way you'll find on your system libpng 1.6.38.git that doesn't have this problem