Thread: X server slaps some template code with a "fatal IO error"

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    X server slaps some template code with a "fatal IO error"

    This code came with Code::Blocks 8, and compiles and runs (most of the way) fine. But as soon as I close the program using window's 'X' button, XLib complains about a fatal IO error.
    Quote Originally Posted by stderr
    XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
    I checked, and the program isn't sent a message beforehand (I did set the event mask to 0x1FFFFF first, yes). The crash *seems* to happen right as I close the window.

    What could be the problem?
    Code:
    /* A simple program to show how to set up an X window for OpenGL rendering.
     * X86 compilation: gcc -o -L/usr/X11/lib   main main.c -lGL -lX11
     * X64 compilation: gcc -o -L/usr/X11/lib64 main main.c -lGL -lX11
     */
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <GL/glx.h>    /* this includes the necessary X headers */
    #include <GL/gl.h>
    
    #include <X11/X.h>    /* X11 constant (e.g. TrueColor) */
    #include <X11/keysym.h>
    
    static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
    static int dblBuf[]  = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
    
    Display   *dpy;
    Window     win;
    GLfloat    xAngle = 42.0, yAngle = 82.0, zAngle = 112.0;
    GLboolean  doubleBuffer = GL_TRUE;
    
    void fatalError(char *message)
    {
      fprintf(stderr, "main: %s\n", message);
      exit(1);
    }
    
    void redraw(void)
    {
      static GLboolean   displayListInited = GL_FALSE;
    
      if (displayListInited)
      {
        /* if display list already exists, just execute it */
        glCallList(1);
      }
      else
      {
        /* otherwise compile and execute to create the display list */
        glNewList(1, GL_COMPILE_AND_EXECUTE);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        /* front face */
        glBegin(GL_QUADS);
          glColor3f(0.0, 0.7, 0.1);  /* green */
          glVertex3f(-1.0, 1.0, 1.0);
          glVertex3f(1.0, 1.0, 1.0);
          glVertex3f(1.0, -1.0, 1.0);
          glVertex3f(-1.0, -1.0, 1.0);
    
          /* back face */
          glColor3f(0.9, 1.0, 0.0);  /* yellow */
          glVertex3f(-1.0, 1.0, -1.0);
          glVertex3f(1.0, 1.0, -1.0);
          glVertex3f(1.0, -1.0, -1.0);
          glVertex3f(-1.0, -1.0, -1.0);
    
          /* top side face */
          glColor3f(0.2, 0.2, 1.0);  /* blue */
          glVertex3f(-1.0, 1.0, 1.0);
          glVertex3f(1.0, 1.0, 1.0);
          glVertex3f(1.0, 1.0, -1.0);
          glVertex3f(-1.0, 1.0, -1.0);
    
          /* bottom side face */
          glColor3f(0.7, 0.0, 0.1);  /* red */
          glVertex3f(-1.0, -1.0, 1.0);
          glVertex3f(1.0, -1.0, 1.0);
          glVertex3f(1.0, -1.0, -1.0);
          glVertex3f(-1.0, -1.0, -1.0);
        glEnd();
        glEndList();
        displayListInited = GL_TRUE;
      }
      if (doubleBuffer)
        glXSwapBuffers(dpy, win);/* buffer swap does implicit glFlush */
      else
        glFlush();  /* explicit flush for single buffered case */
    }
    
    int main(int argc, char **argv)
    {
      XVisualInfo         *vi;
      Colormap             cmap;
      XSetWindowAttributes swa;
      GLXContext           cx;
      XEvent               event;
      GLboolean            needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
      int                  dummy;
    
      /*** (1) open a connection to the X server ***/
    
      dpy = XOpenDisplay(NULL);
      if (dpy == NULL)
        fatalError("could not open display");
    
      /*** (2) make sure OpenGL's GLX extension supported ***/
    
      if(!glXQueryExtension(dpy, &dummy, &dummy))
        fatalError("X server has no OpenGL GLX extension");
    
      /*** (3) find an appropriate visual ***/
    
      /* find an OpenGL-capable RGB visual with depth buffer */
      vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
      if (vi == NULL)
      {
        vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
        if (vi == NULL) fatalError("no RGB visual with depth buffer");
        doubleBuffer = GL_FALSE;
      }
      if(vi->class != TrueColor)
        fatalError("TrueColor visual required for this program");
    
      /*** (4) create an OpenGL rendering context  ***/
    
      /* create an OpenGL rendering context */
      cx = glXCreateContext(dpy, vi, /* no shared dlists */ None,
                            /* direct rendering if possible */ GL_TRUE);
      if (cx == NULL)
        fatalError("could not create rendering context");
    
      /*** (5) create an X window with the selected visual ***/
    
      /* create an X colormap since probably not using default visual */
      cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
      swa.colormap = cmap;
      swa.border_pixel = 0;
      swa.event_mask = KeyPressMask    | ExposureMask
                     | ButtonPressMask | StructureNotifyMask;
      win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0,
                          300, 300, 0, vi->depth, InputOutput, vi->visual,
                          CWBorderPixel | CWColormap | CWEventMask, &swa);
      XSetStandardProperties(dpy, win, "main", "main", None,
                             argv, argc, NULL);
    
      /*** (6) bind the rendering context to the window ***/
    
      glXMakeCurrent(dpy, win, cx);
    
      /*** (7) request the X window to be displayed on the screen ***/
    
      XMapWindow(dpy, win);
    
      /*** (8) configure the OpenGL context for rendering ***/
    
      glEnable(GL_DEPTH_TEST); /* enable depth buffering */
      glDepthFunc(GL_LESS);    /* pedantic, GL_LESS is the default */
      glClearDepth(1.0);       /* pedantic, 1.0 is the default */
    
      /* frame buffer clears should be to black */
      glClearColor(0.0, 0.0, 0.0, 0.0);
    
      /* set up projection transform */
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
      /* establish initial viewport */
      /* pedantic, full window size is default viewport */
      glViewport(0, 0, 300, 300);
    
      printf( "Press left mouse button to rotate around X axis\n" );
      printf( "Press middle mouse button to rotate around Y axis\n" );
      printf( "Press right mouse button to rotate around Z axis\n" );
      printf( "Press ESC to quit the application\n" );
    
      /*** (9) dispatch X events ***/
    
      while (1)
      {
        do
        {
          XNextEvent(dpy, &event);
          switch (event.type)
          {
            case KeyPress:
            {
              KeySym     keysym;
              XKeyEvent *kevent;
              char       buffer[1];
              /* It is necessary to convert the keycode to a
               * keysym before checking if it is an escape */
              kevent = (XKeyEvent *) &event;
              if (   (XLookupString((XKeyEvent *)&event,buffer,1,&keysym,NULL) == 1)
                  && (keysym == (KeySym)XK_Escape) )
                exit(0);
              break;
            }
            case ButtonPress:
              recalcModelView = GL_TRUE;
              switch (event.xbutton.button)
              {
                case 1: xAngle += 10.0;
                  break;
                case 2: yAngle += 10.0;
                  break;
                case 3: zAngle += 10.0;
                  break;
              }
              break;
            case ConfigureNotify:
              glViewport(0, 0, event.xconfigure.width,
                         event.xconfigure.height);
              /* fall through... */
            case Expose:
              needRedraw = GL_TRUE;
              break;
          }
        } while(XPending(dpy)); /* loop to compress events */
    
        if (recalcModelView)
        {
          glMatrixMode(GL_MODELVIEW);
    
          /* reset modelview matrix to the identity matrix */
          glLoadIdentity();
    
          /* move the camera back three units */
          glTranslatef(0.0, 0.0, -3.0);
    
          /* rotate by X, Y, and Z angles */
          glRotatef(xAngle, 0.1, 0.0, 0.0);
          glRotatef(yAngle, 0.0, 0.1, 0.0);
          glRotatef(zAngle, 0.0, 0.0, 1.0);
    
          recalcModelView = GL_FALSE;
          needRedraw = GL_TRUE;
        }
        if (needRedraw)
        {
          redraw();
          needRedraw = GL_FALSE;
        }
      }
    
      return 0;
    }

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Checking through the console, I see that even the Release builds do it too. And this happens with ANY program using XLib that I compile myself, not just this OGL example. But, my GTK+ application doesn't crash. And neither do any other pre-compiled programs I have on my system that I've tried though the console.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Bunch of people have this problem.

    [ubuntu] XIO: fatal IO error 11 (Resource temporarily unavailable) - Ubuntu Forums

    Your distro and/or X server is borked. Ignore and proceed
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    See Ubuntu SUXX!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    >> Your distro and/or X server is borked.
    Why would it be that programs using a GUI FW lib run fine? I mean, the frameworks have to make the sames calls to XLib themselves?

    >> Ignore and proceed
    Unfortunatily, I really can't. I would like to create a program capable of closing a window, and bringing one back up again. Also would like to make it process a close message. Can't do those with this.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I was just looking at that link posted by brewbuck and on the suspicion that these problems are not really related, I compiled and ran this.

    I get the same error when I close it:

    XIO: fatal IO error 11 (Resource temporarily unavailable) on X server ":0.0"
    after 33 requests (22 known processed) with 0 events remaining.
    Segmentation fault


    That's on Fedora core 10-64 so this is not a distro specific problem, it is probably a real error caused by the code. I've never gotten past using GLUT with GL and haven't done any X programming so I can't help with that, but I'm sure there are plenty of other examples around, you should be able to isolate the issue.

    There's no date on there. Could be something minor has changed and so there are bunches of outdated demo code around, which isn't and usual (and neither is demo code that produces errors). Nb. that I don't think you would notice this error during normal operation.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    >> That's on Fedora core 10-64 so this is not a distro specific problem
    Well, only one version below, the same arch, it could be. Except that I've found other posts (not brewbuck's link) that has people that really do seem to have the same problem, running Ubuntu.

    >> I don't think you would notice this error during normal operation.
    What? That's not good enough a reason to ignore it.

    I've downloaded the source for FLTK, maybe I can learn the "right" way of doing this. Let me know if you know of another GUI framework, that uses X, and has less code to go through.
    Thanks for the replies.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Yarin View Post
    >> That's on Fedora core 10-64 so this is not a distro specific problem
    Well, only one version below, the same arch, it could be. Except that I've found other posts (not brewbuck's link) that has people that really do seem to have the same problem, running Ubuntu.

    >> I don't think you would notice this error during normal operation.
    What? That's not good enough a reason to ignore it.
    I think ubuntu is the most widely used distro right now so this could be confusion? I'll check this on FC7-32 and maybe debian this morning, I have to look at them today anyway.

    Vis, ignoring it, I would not be happy with that either. My point was it could be occurring regularly; IMO if you watch the stderr console, X apps have always produced a fair number of errors most of which do not get logged by the system. You are more aware than I of the meaning and significance than this one tho.

    I've downloaded the source for FLTK, maybe I can learn the "right" way of doing this. Let me know if you know of another GUI framework, that uses X, and has less code to go through.
    Thanks for the replies.
    No, I've only ever used GTK, which in terms of programming I would call neither fast nor light. The advantage of that one is you know for sure it is already installed, widely used, and there's lots of support.

    I'm not sure what you are trying to do, but FYI I have written pthreaded stuff with OGL using GLUT and GTK*. General opinion on this is that it blends too many toolkits that are themselves threaded, and that I should drop GLUT and do what you were trying to do above, use X directly. However, I have not had any problems with it at all. GLUT's main loop does I think have to be main(), but GTK's main loop will actually run (and die) as a separate (threaded) function. If you are just learning OGL I would use GLUT to begin with but I'm sure you've heard that.

    *eg an OGL GLUT window with 3D stuff and then a GTK GUI window that controls the 3D stuff via shared variables.
    Last edited by MK27; 08-24-2009 at 07:48 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    As it turns out, FLTK was a good choice. I was able to find what I was look for there.

    Solution:

    With XChangeProperty(), add the WM_DELETE_WINDOW message to the WM_PROTOCOLS events. Your program will then receive the close messages, through which you can end your window gracefully.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference Counting
    By Dae in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2009, 07:34 AM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM