We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2884 - SDL_Quit() crashes C program (gcc 4.8.2) because of Xlib (libX11.so.6)
Summary: SDL_Quit() crashes C program (gcc 4.8.2) because of Xlib (libX11.so.6)
Status: RESOLVED INVALID
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: 2.0.2
Hardware: x86_64 Linux
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-02-18 19:47 UTC by Rustam
Modified: 2015-06-08 05:38 UTC (History)
2 users (show)

See Also:


Attachments
Here's the test binary for Linux (>=3.0) (19.85 KB, application/x-executable)
2015-02-19 13:04 UTC, Rustam
Details
libX11.so.6 from my OS distribution (may cause the problem) (1.21 MB, application/x-sharedlib)
2015-02-19 13:08 UTC, Rustam
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rustam 2015-02-18 19:47:33 UTC
Attempting to compile any SDL2 code that creates window, destroys it and then calling SDL_Quit will result either in a segmentation fault (139) or bus error if it's written in C and compiled using GCC (4.8.2, though version number doesn't affect this behaviour). Here's the testing code for the issue:

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

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *picture = NULL;

int init(void) {
        int success = 1;
        if(SDL_Init(SDL_INIT_VIDEO) < 0) {
                printf("Failed to initialize SDL: %s\n", SDL_GetError());
                success = 0;
        } else {
                window = SDL_CreateWindow("SDL Test",
                        SDL_WINDOWPOS_UNDEFINED, \
                        SDL_WINDOWPOS_UNDEFINED, \
                        SCREEN_WIDTH, SCREEN_HEIGHT, \
                        SDL_WINDOW_SHOWN
                );

                if(window == NULL) {
                        printf("Failed to create window: %s\n", SDL_GetError());
                        success = 0;
                } else {
                        screen = SDL_GetWindowSurface(window);
                }
        }

        return success;
}

//int loadMedia();
void shutdown(void) {
        /*SDL_FreeSurface(picture);
        picture = NULL;*/

        SDL_DestroyWindow(window);
        window = NULL;

        SDL_Quit();
}

int main(int argc, char *args[]) {
        if(!init()) {
                printf("Init failed!\n");
        } else {
                //SDL_Delay(2000);
        }

        shutdown();
        return 0;
}

Compiled with g++, it works as intended and there's no segfault or bus error. The program itself works flawlessly until I call SDL_Quit:

Program received signal SIGBUS, Bus error.
0x00007ffff6ba6895 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
(gdb) backtrace
#0  0x00007ffff6ba6895 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#1  0x00007ffff6adde02 in XCloseIM ()
   from /usr/lib/x86_64-linux-gnu/libX11.so.6
#2  0x00007ffff7b93d3b in X11_VideoQuit (_this=<optimized out>)
    at /home/rustam/src/SDL2-2.0.2/src/video/x11/SDL_x11video.c:652
#3  0x00007ffff7b7bacb in SDL_VideoQuit_REAL ()
    at /home/rustam/src/SDL2-2.0.2/src/video/SDL_video.c:2352
#4  0x00007ffff7ad6ac6 in SDL_QuitSubSystem_REAL (flags=<optimized out>)
    at /home/rustam/src/SDL2-2.0.2/src/SDL.c:297
#5  SDL_Quit_REAL () at /home/rustam/src/SDL2-2.0.2/src/SDL.c:357
#6  0x00000000004009c4 in shutdown () at main.c:43
#7  0x00007ffff687f786 in xcb_disconnect ()
   from /usr/lib/x86_64-linux-gnu/libxcb.so.1
#8  0x00007ffff6aaf4e7 in XCloseDisplay ()
   from /usr/lib/x86_64-linux-gnu/libX11.so.6
#9  0x00007ffff7b93c9a in X11_DeleteDevice (device=0x607ad0)
    at /home/rustam/src/SDL2-2.0.2/src/video/x11/SDL_x11video.c:324
#10 0x00007ffff7b7bc2d in SDL_VideoQuit_REAL ()
    at /home/rustam/src/SDL2-2.0.2/src/video/SDL_video.c:2377
#11 0x00007ffff7ad6ac6 in SDL_QuitSubSystem_REAL (flags=<optimized out>)
    at /home/rustam/src/SDL2-2.0.2/src/SDL.c:297
#12 SDL_Quit_REAL () at /home/rustam/src/SDL2-2.0.2/src/SDL.c:357
#13 0x00000000004009c4 in shutdown () at main.c:43
#14 0x00000000004009ed in main (argc=1, args=0x7fffffffe198) at main.c:53

At first I blamed Xlib and was looking for the way to switch SDL2 to XCB, but then forum people advised me to file a bug, because it might be SDL problem. So I hope some will help me out on this annoying issue, which is present for at least 1.5 years and makes my work with SDL2 quite painful.

This issue is not present w/ SDL 1.2.
Comment 1 Rustam 2015-02-19 03:51:33 UTC
Forgot to mention: Xlib version is 1.6.2 (from X11R7.7), the bug appeared for me after  upgrading to release version of SDL2 (2.0.0 or so). Distribution is Ubuntu 14.04, it is possible that somebody from packaging/maintaining team have spoiled either X11 or SDL package. I have little experience in programming, so debugging Xlib became a big trouble for me
Comment 2 Rustam 2015-02-19 13:04:57 UTC
Created attachment 2035 [details]
Here's the test binary for Linux (>=3.0)

This is compiled source from original bug report. Reproduces problem running on Ubuntu 14.04
Comment 3 Rustam 2015-02-19 13:08:05 UTC
Created attachment 2036 [details]
libX11.so.6 from my OS distribution (may cause the problem)
Comment 4 Ryan C. Gordon 2015-06-08 05:38:23 UTC
(In reply to Rustam from comment #0)
> #6  0x00000000004009c4 in shutdown () at main.c:43

Name your "shutdown" function something else. That's a standard C runtime function, which Xlib wants to call to close a socket, but it's calling your totally-unrelated function by accident.

This happens in C and not C++ because C++ name-mangles it to a different symbol, where as C leaves it as "shutdown" and disaster ensues.

--ryan.