cposixx11

What is the point of XCloseDisplay() man page saying that it may generate a BadGC error, if the return value can only ever be 0?


I was making a return value check, but then I read the source code and found out that the function doesn't return the error, but just may or may not generate it (via XFreeGC(), I guess) and you will never know unless you explicitly check for it, but since you are running a final destroyer function, why would you ever care.

XCloseDisplay can generate a BadGC error.

int
XCloseDisplay (
    register Display *dpy)
{
    register _XExtension *ext;
    register int i;

    if (!(dpy->flags & XlibDisplayClosing))
    {
        dpy->flags |= XlibDisplayClosing;
        for (i = 0; i < dpy->nscreens; i++) {
            register Screen *sp = &dpy->screens[i];
            XFreeGC (dpy, sp->default_gc);
        }
        if (dpy->cursor_font != None) {
        XUnloadFont (dpy, dpy->cursor_font);
        }
        XSync(dpy, 1);  /* throw away pending events, catch errors */
        /* call out to any extensions interested */
        for (ext = dpy->ext_procs; ext; ext = ext->next) {
        if (ext->close_display)
            (*ext->close_display)(dpy, &ext->codes);
        }
        /* if the closes generated more protocol, sync them up */
        if (X_DPY_GET_REQUEST(dpy) != X_DPY_GET_LAST_REQUEST_READ(dpy))
        XSync(dpy, 1);
    }
    xcb_disconnect(dpy->xcb->connection);
    _XFreeDisplayStructure (dpy);
    return 0;
}

Solution

  • libX11 allows you to register an error handler callback that is executed immediately when the error event is received using the function XSetErrorHandler. If you don't have an error handler installed a default handler is invoked, which will print the error. When closing a display there still might be operations pending that are tied to a graphics context; in that case a BadGC error happens, since those operations cannot be finalized to completion, which will then invoke the installed error handler.