Clipboard trouble under X using Scrap


I’m trying to add clipboard support to the Linux version of Docking
Station, using the SDL Scrap code from

Everything works fine when I paste text that was copied from my own
application, but it crashes if I paste text copied from Gnome Terminal
(for example).

What I’ve done is take scrap.c and scrap.h, compile them into my
project and call get_scrap on a key press.

It gives the following error:

Gdk-ERROR **: BadWindow (invalid Window parameter)
serial 140 error_code 3 request_code 24 minor_code 0

Then I have a breakpoint on a global destructor (which strangely gets
called), and it gives the following stack trace:

(gdb) bt
#0 Camera::~Camera (this=0x40db5940, __in_chrg=2) at
#1 0x40d834b5 in MainCamera::~MainCamera (this=0x40db5940,
__in_chrg=2) at engine/Camera/MainCamera.cpp:95
#2 0x40d84025 in __tcf_1 () at engine/Camera/MainCamera.cpp:576
#3 0x40e07655 in exit () from /lib/
#4 0x4017e9e3 in gdk_send_xevent () from /usr/lib/
#5 0x40f57713 in _XError () from /usr/X11R6/lib/
#6 0x40f5637d in _XReply () from /usr/X11R6/lib/
#7 0x40f51a38 in XSync () from /usr/X11R6/lib/
#8 0x402184b6 in unlock_display () at SDL_x11wm.c:321
#9 0x40b8ad04 in get_scrap (type=1413830740, dstlen=0xbffff4f4,
at engine/Display/SDL/scrap.cpp:387
#10 0x404f7800 in InputManager::FetchTextFromClipboard
(this=0x405a7e10) at engine/InputManager.cpp:273
#11 0x40847ae8 in UIText::RawKey (this=0x84e1498, keyCode=86) at
#12 0x404dec74 in App::HandleInput (this=0x405a7da0) at
#13 0x404dcc26 in App::UpdateApp (this=0x405a7da0) at
#14 0x0805215c in main (argc=1, argv=0xbffff71c) at
#15 0x40df40de in __libc_start_main () from /lib/

As you can see, the unlock_display call fails. In particular it is
the call in this piece of code from Sam’s scrap.c:

selection = XInternAtom(SDL_Display, “SDL_SELECTION”, False);
XConvertSelection(SDL_Display, XA_PRIMARY, format,
selection, owner, CurrentTime);

If I remove the line XConvertSelection it just freezes - presumably on
the next lines which await a response. The top level of the call
stack is in main(), and in a loop which calls SDL functions to get
events and deal with them. My App is also linked to GDK and GTK, and
uses them for dialogs sometimes.

Does anyone have any idea why this is happening? I know nothing about
X, and I’m sure I must be something simple I’m not doing.

Thanks for any help,