Common sense (and the fact that the error says BadRegion here) suggest that reg = XFixesCreateRegion( display, rec, 1); This region number is no longer valid after you close the display. Doing this a 2nd time after setting nRect = 0 will fix it. Maarten.