[comp.windows.x] XFreeColors bug

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

The following program demonstrates an XFreeColors Bug in the X11R4 library.
If one creates a private colormap and allocates cells in it via the create
call, those cells cannot be freed.
However, if the cells are not allocated during the create, but are instead
specifically allocated later, they can be freed.
================================  cut here  ===================
/*
 *	If:
 *		A colormap is created and all cells are allocated in the create call
 *	Then:
 *		An attempt to free one of the allocated cells will fail.
 */
#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("allocated pixel: %d\n",pxl) ;
		pxl = 0 ;
		XFreeColors(dpy, color_map, &pxl, 1, 0) ;
		printf("Color cell allocated in user code free ok\n") ;
	}
	if (color_map = XCreateColormap(dpy, win, DefaultVisual(dpy,screen), AllocAll)) {
		pxl = 0 ;
		XFreeColors(dpy, color_map, &pxl, 1, 0) ;
		printf("Color cell allocated in Xlib free ok\n") ;
	}
}
--
Gary Aitken

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

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

> The following program demonstrates an XFreeColors Bug in the X11R4
> library.  If one creates a private colormap and allocates cells in it
> via the create call, those cells cannot be freed.  However, if the
> cells are not allocated during the create, but are instead
> specifically allocated later, they can be freed.

[edited down -dM]
> 	if (color_map = XCreateColormap(dpy, win, DefaultVisual(dpy,screen), AllocNone)) {
> 		XAllocColorCells(dpy, color_map, True, 0, 0, &pxl, 1);
> 		XFreeColors(dpy, color_map, &pxl, 1, 0) ;
> 	}
> 	if (color_map = XCreateColormap(dpy, win, DefaultVisual(dpy,screen), AllocAll)) {
> 		pxl = 0 ;
> 		XFreeColors(dpy, color_map, &pxl, 1, 0) ;
> 	}

This is not a bug.  From the Xlib documentation:

	To create a colormap for a screen, use XCreateColormap.
	
	Colormap XCreateColormap(display, w, visual, alloc)
[...]
	alloc     Specifies the colormap entries to be allocated.
	          You can pass AllocNone or AllocAll.
[...]
	If alloc is AllocAll, the entire colormap is allocated
	writable.  The initial values of all allocated entries are
	undefined.  For GrayScale and PseudoColor, the effect is as if
	[...].  For DirectColor, the effect is as if [...].  However,
	in all cases, none of these entries can be freed by using
	XFreeColors.

					der Mouse

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

pinkas@st860.intel.com (Israel Pinkas) (12/05/90)

In article <1990Nov30.205231.21765@Solbourne.COM> garya@garya.Solbourne.COM (Gary Aitken) writes:

> The following program demonstrates an XFreeColors Bug in the X11R4 library.
> If one creates a private colormap and allocates cells in it via the create
> call, those cells cannot be freed.
> However, if the cells are not allocated during the create, but are instead
> specifically allocated later, they can be freed.
> ================================  cut here  ===================
> /*
>  *	If:
>  *	   A colormap is created and all cells are allocated in the create call
>  *	Then:
>  *	   An attempt to free one of the allocated cells will fail.
>  */

[Code deleted]

Funny, that's what my documentation say it will do.  The man page states:

	However, in all cases, none of these entries can be
     freed by using XFreeColors.
 
-Israel Pinkas
 Intel Corp
--
--------------------------------------
Disclaimer: The above are my personal opinions, and in no way represent
the opinions of Intel Corporation.  In no way should the above be taken
to be a statement of Intel.

UUCP:	{amdcad,decwrl,hplabs,oliveb,pur-ee,qantel}!intelca!mipos3!st860!pinkas
ARPA:	pinkas%st860.intel.com@relay.cs.net
CSNET:	pinkas@st860.intel.com