Xlib: XKeysymToKeycode hangs

Two simple questions related to the following puzzling behaviour:

I have the following line of code:

   const KeyCode kc = XKeysymToKeycode(_display_p, ks);

where _display_p is a valid Display* returned from XOpenDisplay() and ks has the value 103 (which was returned by XStringToKeysym(), and corresponds to 'g').

The above line of code hangs indefinitely; I have to kill the job manually.

Question #1: why is this happening? I can't see anything in any documentation that suggests that XKeysymToKeycode() can ever fail to return.

Question #2: how do I stop the call from hanging?

Ok - this is C, you assert the _display_p variable is valid. You know this how?

Please post your code.

The pertinent class is called keyboard_queue.

In the class definition, there is:

  Display*                    _display_p;   // the X display pointer

The constructor provides a value for this in the initializer list:

  _display_p(XOpenDisplay(NULL)),

The value is tested inside the construstor, just in case the call to XOpenDisplay() failed:

 if (!_display_p)
  { cerr << "Fatal error: unable to open display" << endl;
    exit (-1);
  }

Then, inside a member function called push_key_press, occurs the line that hangs (I have temporarily, while testing, replaced the call to the variable ks of type KeySym with an explicit value, XK_g):

   const KeyCode kc = XKeysymToKeycode(_display_p, XK_g);

Following the initialisation of _display_p in the class constructor, the only place that _display_p is used is in direct calls to X functions: I never access it myself.

If I remove the line that hangs, and code that depends on it, then everything else -- in particular, all the calls to X functions, both prior to and subsequent to the offending line -- works as expected.