SDL_Init segfaulting

A very simple code is segfaulting

#include <SDL2/SDL.h>
#include <stdio.h>

void close();

SDL_Window * Window = NULL;

int main(int argc, char *argv[])
    int retval = 0;
    retval = SDL_Init(SDL_INIT_VIDEO);
    if (retval < 0)
        return 1;
    } else {
        Window = NULL;
    return 0;

void close()
    Window = NULL;

The funny thing is, if I remove the void close() function and the prototype, even not calling it. The program runs without segfault.

The valgdrind log is a bit long weirdbehavior.txt (65.1 KB)

The ldd output is (0x00007ffc6bb74000) => /lib/x86_64-linux-gnu/ (0x00007f35dff69000) => /lib/x86_64-linux-gnu/ (0x00007f35dfda6000) => /lib/x86_64-linux-gnu/ (0x00007f35dfcad000) => /lib/x86_64-linux-gnu/ (0x00007f35dfb68000) => /lib/x86_64-linux-gnu/ (0x00007f35dfb63000) => /lib/x86_64-linux-gnu/ (0x00007f35dfb0f000) => /lib/x86_64-linux-gnu/ (0x00007f35df9cb000) => /lib/x86_64-linux-gnu/ (0x00007f35df7b9000) => /lib/x86_64-linux-gnu/ (0x00007f35df7ac000) => /lib/x86_64-linux-gnu/ (0x00007f35df7a7000) => /lib/x86_64-linux-gnu/ (0x00007f35df795000) => /lib/x86_64-linux-gnu/ (0x00007f35df58a000) => /lib/x86_64-linux-gnu/ (0x00007f35df583000) => /lib/x86_64-linux-gnu/ (0x00007f35df37d000) => /lib/x86_64-linux-gnu/ (0x00007f35df378000) => /lib/x86_64-linux-gnu/ (0x00007f35df367000) => /lib/x86_64-linux-gnu/ (0x00007f35df35e000) => /lib/x86_64-linux-gnu/ (0x00007f35df31b000) => /lib/x86_64-linux-gnu/ (0x00007f35df2f8000)
/lib64/ (0x00007f35e00fd000) => /usr/lib/x86_64-linux-gnu/pulseaudio/ (0x00007f35df274000) => /lib/x86_64-linux-gnu/ (0x00007f35df221000) => /lib/x86_64-linux-gnu/ (0x00007f35df1f7000) => /lib/x86_64-linux-gnu/ (0x00007f35defed000) => /lib/x86_64-linux-gnu/ (0x00007f35defe3000) => /lib/x86_64-linux-gnu/ (0x00007f35defd7000) => /lib/x86_64-linux-gnu/ (0x00007f35def29000) => /lib/x86_64-linux-gnu/ (0x00007f35def1d000) => /lib/x86_64-linux-gnu/ (0x00007f35deea2000) => /lib/x86_64-linux-gnu/ (0x00007f35dee98000) => /lib/x86_64-linux-gnu/ (0x00007f35dee8d000) => /lib/x86_64-linux-gnu/ (0x00007f35dec89000) => /lib/x86_64-linux-gnu/ (0x00007f35dea83000) => /lib/x86_64-linux-gnu/ (0x00007f35dea5a000) => /lib/x86_64-linux-gnu/ (0x00007f35dea36000) => /lib/x86_64-linux-gnu/ (0x00007f35de919000) => /lib/x86_64-linux-gnu/ (0x00007f35de8ff000) => /lib/x86_64-linux-gnu/ (0x00007f35de8c0000) => /lib/x86_64-linux-gnu/ (0x00007f35de6b7000) => /lib/x86_64-linux-gnu/ (0x00007f35de689000) => /lib/x86_64-linux-gnu/ (0x00007f35de5dc000) => /lib/x86_64-linux-gnu/ (0x00007f35de5c4000) => /lib/x86_64-linux-gnu/ (0x00007f35de5aa000) => /lib/x86_64-linux-gnu/ (0x00007f35de584000)

Using SDL2 version from system

$ dpkg -l | grep sdl | awk '{printf ("    %35s\t%20s\n",$2,$3)}'
              libsdl-image1.2:amd64	           1.2.12-12
              libsdl-ttf2.0-0:amd64	            2.0.11-6
              libsdl1.2debian:amd64	      1.2.15+dfsg2-5
                libsdl2-2.0-0:amd64	      2.0.12+dfsg1-1
                 libsdl2-2.0-0:i386	      2.0.12+dfsg1-1
         libsdl2-2.0-0-dbgsym:amd64	      2.0.12+dfsg1-1
                  libsdl2-dev:amd64	      2.0.12+dfsg1-1
            libsdl2-gfx-1.0-0:amd64	     1.0.4+dfsg-3+b1
     libsdl2-gfx-1.0-0-dbgsym:amd64	     1.0.4+dfsg-3+b1
              libsdl2-gfx-dev:amd64	     1.0.4+dfsg-3+b1
                    libsdl2-gfx-doc	        1.0.4+dfsg-3
          libsdl2-image-2.0-0:amd64	       2.0.5+dfsg1-2
   libsdl2-image-2.0-0-dbgsym: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-2.0-0-dbgsym: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-2.0-0-dbgsym: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-2.0-0-dbgsym:amd64	      2.0.15+dfsg1-1
              libsdl2-ttf-dev:amd64	      2.0.15+dfsg1-1

Is this a bug?

Yes, this is a bug - in your code :stuck_out_tongue:

The problem is that close() is a standard C function that is (at least indirectly) used by SDL itself.
You’re basically overriding it with your own close() function, so when SDL tries to call C close() weird things happen (shutdown() is another candidate for bugs like this).
Making it static should fix the issue, or if you need to access it from other .c files rename it (it’s a good idea to use some project-wide prefix for all non-static functions and also types)

Or use C++ and just put everything in your own namespace (though C++ name mangling makes such collisions a lot less likely even without namespaces)

1 Like

Hell… Thanks. So stupid error.

Ohh. no… pls. I can pick other names.

Do you know if GCC has any compilation flag that I can enable to warn about these mistakes?

Hell… Thanks. So stupid error.

You’re certainly not the first to run into this, I know I have :slight_smile:

Do you know if GCC has any compilation flag that I can enable to warn about these mistakes?

I’m not aware of any, but I haven’t searched for one too much - maybe the linker could catch this?
(It couldn’t be perfect, future versions of system libs could introduce new symbols your code can collide with that can’t be detected when you build it, but it’d be better than nothing)
If you find something please share :wink:

1 Like

I’ve been caught by this too; the failure mode can be frustratingly obscure, and code can even run (apparently) correctly if your ‘replacement’ function does something sufficiently similar to the system one! I don’t know of a linker option (other than C+±specific ones) to detect it, but I’d sure like there to be.

1 Like