Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!bloom-beacon!husc6!rice!kalliope!bbc From: bbc@lutetia.rice.edu (Benjamin Chase) Newsgroups: comp.windows.x Subject: Re: XCreateImage/XDestroyImage question Message-ID: Date: 17 Jun 89 22:25:57 GMT References: <1202@masscomp.UUCP> <14738@pasteur.Berkeley.EDU> Sender: usenet@rice.edu Distribution: usa Organization: Rice University Lines: 33 In-reply-to: dheller@cory.Berkeley.EDU's message of 17 Jun 89 17:42:34 GMT 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