简体   繁体   中英

Segmentation fault when using Derelict SDL

I am trying to use Derelict and D to write a simple graphics test program.

When I try to do nearly anything with SDL it will seg-fault. Here is the code that is having issues:

import std.stdio;   
import derelict.opengl3.gl3; 
import derelict.sdl2.sdl;
import derelict.sdl2.image;
import derelict.sdl2.mixer;
import derelict.sdl2.ttf;
import derelict.sdl2.net;

void main()
{
    SDL_Window* mainWindow;
    SDL_GLContext mainGLContext;

    try
    {
        DerelictGL3.load();

        // Load the SDL 2 library.
        DerelictSDL2.load();
.
        DerelictSDL2Image.load();
        DerelictSDL2Mixer.load();
        DerelictSDL2ttf.load();
        DerelictSDL2Net.load();
    }
    catch(Exception e){}
    finally{}

    // Initialise SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
        throw new Exception("SDL initialization failed");
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,3);

    Uint32 flags = SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL;
    int width = 1024;
    int height = 768;

    mainWindow = SDL_CreateWindow("SDL2 OpenGL Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags);
    mainGLContext = SDL_GL_CreateContext(mainWindow);

    DerelictGL3.reload();

    SDL_DestroyWindow(mainWindow);
    SDL_Quit();
}

The issue will still occur even if everything below SDL_GL_SetAttribute() is commented out.

Additionally, derelict throws an exception when trying load SDL, but I think this is fairly common:

derelict.util.exception.SymbolLoadException@../../.dub/packages/derelict-util-2.0.4/source/derelict/util/exception.d(35): Failed to load symbol SDL_QueueAudio from shared library libSDL2.so

I am running Elementary OS and have used apt-get to ensure that SDL is up to date.

Thank you very much for your help.

This is almost certainly down to SDL versioning issues.

My recommendation is to build the whole thing from source. It's not difficult by any means.

You can get SDL2 source here:

https://www.libsdl.org/download-2.0.php

Ensure you also get the extra projects too, ie sdl2 image, net, mixer and ttf. Links for these are at:

https://www.libsdl.org/projects/

To install them, your usual

./configure
make
sudo make install

will do the trick. Do SDL2 first, then the rest in whatever order you like.

I had this problem on Debian/Fedora/Ubuntu too. It is finding SDL 1.2 instead of your SDL 2.X and failing to bind to that. You need to specify what SDL version to use. Derelict should work fine with any 2.X version of SDL. You should not have to build SDL on any big name Linux distro. Do this to fix it:

// Change this
DerelictSDL2.load();
// To this
DerelictSDL2.load(SharedLibVersion(2, 0, 2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM