Xlib Problem: XCloseDisplay seg fault

Hi,
First of all forgive me if Xlib related problems does not go under this thread.

In my main program, im using Xlib`s XImage type object which contains a regularly updating bitmap, and maps the XImage to a Xwindow using XPutImage.
( XCreateImage is used to create the XImage. )

The program also have another thread as the X-Event handler. there at an expose event, XPutImage is used to update the XWindow.

The XLib functions works fine and does the job, but the problems occurs when tried to exit the main, before exiting im calling the following functions in order to release the X-System resources taken.

	/* Destroy the XImage - de alloc memory */
	XDestroyImage( xImage );

	/* destroy specified window */
	XDestroyWindow( pDisplay, showWindow );
		
	/* disconnect from X server */
	XCloseDisplay( pDisplay );    -------> give a segmentation fault

When @ the XCloseDisplay( pDisplay ); function call it gives a segmentation fault.
I tried different solutions but never went right. can anybody have any comments on what im doing wrong here.
Thankx.