[comp.windows.x] XCreateImage/XDestroyImage question

bbc@lutetia.rice.edu (Benjamin Chase) (06/18/89)

In article <1202@masscomp.UUCP>, karenb@masscomp.uucp asks a
reasonable question that needs a good answer:
> You create an image using XCreateImage and the statically defined image
> data... The you're done with the image and want to free its structure.
> XDestroyImage will free both the image structure and the image data,
> but you don't want to free the image data because it's static...
> Should you set the data pointer in the image structure to NULL?  Or call
> Xfree on the image structure?

And in two followups, dheller@cory.berkeley.edu misses the boat...
> In my last response, I said you should set the data = malloc(1).
> You don't need to do that -- _XDestroyImage checks against NULL
> before it frees the image.

What Karen is obviously concerned with is violating an abstraction.
To set the data pointer to "malloc(1)", or NULL, or anything, implies
that she mention field names of the image structure in her code.  To
Xfree the image structure is to assume that Xfree is sufficient to
cleanup and deallocate an unwanted image made from static data.

We had the same problem here some time ago, when porting from X10R4 to
X11R3, and chose NULLing the pointer as the least stupid and obnoxious
hack.  We put a nice comment next to the hack, so when that code fails
to compile because the representation of images changes, the person
maintaining our code can figure out what and why.  But there ought to
be a better way.  Perhaps a function to install new image data in an
existing image, and allow NULL as the new image data?  I haven't
looked at image handling recently enough to be sure this is a
reasonable proposal, so don't come yelling and screaming to me if it
doesn't make sense.
--

	Ben Chase, Computer Science, Rice University, Houston, Texas

rws@EXPO.LCS.MIT.EDU (06/19/89)

Repeat after me: the XImage interface is a crock, the XImage interface is a ...

We really ought to fix it someday.

The simplest thing is indeed just to store NULL before calling XDestroyImage.

rws@EXPO.LCS.MIT.EDU (06/19/89)

    I can't say for sure, but
    it sounds like you're making assumptions on the bit/byte ordering of your
    image and when you eventually try to XPutImage this thing to a drawable,
    you might not get what you want if you are talking to an X server on a
    different architecture.

This is incorrect.  XPutImage goes to great pains to handle transformation
from source bit/byte ordering to the server's, and in fact to handle
bits-per-pixel differences.  So long as the depths match, there is nothing
inherently evil with using canned images.  The only tradeoff is that you
will take the transformation cost on every XPutImage call, rather than
once up front.

dheller@cory.Berkeley.EDU (Dan Heller) (07/23/89)

In article karenb@masscomp.UUCP (Karen ) writes:
> 	1. You have some image data statically defined, say in an array.  By
> 	   static I mean it wasn't allocated using malloc.

I question whether this is the best thing to do.  I can't say for sure, but
it sounds like you're making assumptions on the bit/byte ordering of your
image and when you eventually try to XPutImage this thing to a drawable,
you might not get what you want if you are talking to an X server on a
different architecture.

I can certainly understand the motivation, however.  If I can venture a
guess as to why you want to do this --it's because it's very expensive to
create a large pixmap, send the whole thing over the wire to the client,
twiddle the bits, send it back and free the pixmap.  There are four very
expensive operations here:
    creating the pixmap
    sending it to client
    sending it back to server
    freeing memory

> Is there a "best" way to do this?  Should you set the data pointer in the image
> structure to NULL before calling XDestroyImage?  Or call Xfree on the image
> structure?  Or am I totally missing something?

Just as a thought -- you could set the data pointer to malloc(1) :-}

> 		   /__/_______/  /	karenb@westford.ccur.com

Dan Heller	<island!argv@sun.com>

dheller@cory.Berkeley.EDU (Dan Heller) (07/23/89)

>   Should you set the data pointer in the image
> structure to NULL before calling XDestroyImage?

In my last response, I said you should set the data = malloc(1).
You don't need to do that -- _XDestroyImage checks against NULL
before it frees the image.

Dan Heller	<island!argv@sun.com>

karenb@masscomp.UUCP (Karen ) (07/23/89)

I have a question about XCreateImage and XDestroyImage.  What are you supposed
to do in the following scenario?

	1. You have some image data statically defined, say in an array.  By
	   static I mean it wasn't allocated using malloc.
	2. You create an image using XCreateImage and the statically defined
	   image data.  XCreateImage allocates and sets up the image structure.
	3. Then you're done with the image and want to free its structure.
	   XDestroyImage will free both the image structure and the image data,
	   but you don't want to free the image data because it's static.

Is there a "best" way to do this?  Should you set the data pointer in the image
structure to NULL before calling XDestroyImage?  Or call Xfree on the image
structure?  Or am I totally missing something?

Thanks for any help,
Karen

--------------------------------------------------------------------------------
		     ___________
		    /  ________/__	...!{decvax,uunet}!masscomp!karenb
		   /__/_______/  /	karenb@westford.ccur.com
	  Concurrent /__________/
	Computer Corporation

--------------------------------------------------------------------------------

rws@EXPO.LCS.MIT.EDU (08/02/89)

    Should you set the data pointer in the image
    structure to NULL before calling XDestroyImage?

Yup, afraid so.