[comp.windows.x] XAllocColor bug

garya@garya.Solbourne.COM (Gary Aitken) (12/01/90)

The following program demonstrates a bug when trying to
allocate a color cell.
This is in the X11R4 server.
===================== cut here ======================
/*
 *	If:
 *		A color cell is allocated using XAllocColorCells
 *		The allocated color cell is configured using XStoreColor
 *	Then:
 *		An attempt to use the same color cell via XAllocColor will fail in one of two ways:
 *			If additional color cell is available:	(The case in the program below)
 *				A new one will be allocated.
 *			Else:
 *				XAllocColor will fail (return 0)
 *		Both of these are bugs; in both cases, the previously allocated cell should be returned
 */
#include <X11/X.h>
#include <X11/Xlib.h>

 main (argc, argv)
			int		argc ;
			char		**argv ;
{
			Display		*dpy ;
			int		screen;
			Window		win ;
			XColor		clr_def ;
			Colormap	color_map ;
			Colormap	def_color_map ;
			unsigned long	pxl ;


	dpy = XOpenDisplay(0);
	screen = DefaultScreen(dpy);
	def_color_map = DefaultColormapOfScreen(ScreenOfDisplay(dpy,screen)) ;
	XSynchronize(dpy,1) ;
	win = XDefaultRootWindow(dpy) ;
	if (color_map = XCreateColormap(dpy, win, DefaultVisual(dpy,screen), AllocNone)) {
		XAllocColorCells(dpy, color_map, True, 0, 0, &pxl, 1);
		printf("pixel allocated: %d\n",pxl) ;
		clr_def.pixel = pxl ;
		clr_def.red = 0 ;
		clr_def.green = 0 ;
		clr_def.blue = 0 ;
		clr_def.flags = DoRed|DoGreen|DoBlue ;
		XStoreColor(dpy, color_map, clr_def);
		if (XAllocColor(dpy,color_map,&clr_def))
			printf("pixel: %d\n",clr_def.pixel) ;
	}
}
--
Gary Aitken

Solbourne Computer Inc.    ARPA: garya@Solbourne.COM
Longmont, CO               UUCP: !{boulder,sun}!stan!garya

garya@garya.Solbourne.COM (Gary Aitken) (12/01/90)

Oops.  The previously posted bug relating to XAllocColor is incorrect.
The color cells should have been allocated in the
first place using XAllocColor to get read only color cells, instead
of XAllocColorCells to get read/write cells.  So it's not a bug.

--
Gary Aitken

Solbourne Computer Inc.    ARPA: garya@Solbourne.COM
Longmont, CO               UUCP: !{boulder,sun}!stan!garya

etaylor@fleming.iaims.bcm.tmc.edu (Eric Taylor) (12/01/90)

The program fragment you posted
fails to pass a pointer to XStoreColor.
You passed a whole structure instead
of a pointer.
--
					Eric Taylor
					Baylor College of Medicine
					etaylor@wilkins.bcm.tmc.edu
					(713) 798-3776

furr@mdavcr.UUCP (Steven Furr) (12/01/90)

garya writes: 

> /*
>  *	If:
>  *		A color cell is allocated using XAllocColorCells
>  *		The allocated color cell is configured using XStoreColor
>  *	Then:
>  *		An attempt to use the same color cell via XAllocColor will fail
>                in one of two ways:
>  *			If additional color cell is available:	(The case in
>                                                          the program below)
>  *				A new one will be allocated.
>  *			Else:
>  *				XAllocColor will fail (return 0)
>  *		Both of these are bugs; in both cases, the previously allocated cell should be returned
>  */

If you refer to the manual you will find that XAllocColorCells allows you
to allocate read/write cells.  The client allocating these cells is free
to change the pixel value with XStoreColors.  For this reason, the server
considers these cells to be non-shareable and will NOT return one of them
to any client using an XAllocColor call.  If it did, it is quite possible
that the client owning the cell might change the value later and any
clients using it would have incorrect colours displayed.

It is possible for another client to make use of the pixel value for the
cell (assuming it knows where it is or can find it -- with XQueryColors
for example), but the server is not going to provide any assistance since
it is considered a violation of conventions.

mouse@LIGHTNING.MCRCIM.MCGILL.EDU (12/01/90)

> The following program demonstrates a bug when trying to allocate a
> color cell.  This is in the X11R4 server.

>  *	If:
>  *		A color cell is allocated using XAllocColorCells
>  *		The allocated color cell is configured using XStoreColor
>  *	Then:
>  *		An attempt to use the same color cell via XAllocColor will fail in one of two ways:
>  *			If additional color cell is available:	(The case in the program below)
>  *				A new one will be allocated.
>  *			Else:
>  *				XAllocColor will fail (return 0)
>  *		Both of these are bugs; in both cases, the previously allocated cell should be returned

This is not a bug; this is how it's supposed to work.  XAllocColorCells
allocates read/write cells; XAllocColor is defined to never return a
read/write cell.  If you allocate the initial cell with XAllocColor, so
as to get a read-only cell, then a second XAllocColor should, and
undoubtedly will, return the same cell.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu