Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XDestroyImage()

10 views
Skip to first unread message

Mut...@dastardlyhq.com

unread,
Nov 12, 2023, 10:59:38 AM11/12/23
to
Hello

I'm a bit uncertain from the man page and from googling around whether
XDestroyImage() only frees the XImage structure created by XCreateImage()
or whether it also frees the user allocated memory that gets passed into
XCreateImage().

Anyone know?

Thanks for any help

Kalevi Kolttonen

unread,
Nov 12, 2023, 11:20:11 AM11/12/23
to
In comp.unix.programmer Mut...@dastardlyhq.com wrote:
> I'm a bit uncertain from the man page and from googling around whether
> XDestroyImage() only frees the XImage structure created by XCreateImage()
> or whether it also frees the user allocated memory that gets passed into
> XCreateImage().

I just did the following on Fedora Linux 39:

dnf download --source libX11-devel
rpm2cpio libX11-1.8.7-1.fc39.src.rpm | cpio -idm
unxz libX11-1.8.7.tar.xz
tar xvf libX11-1.8.7.tar

and some quick grepping. Here are the relevant results:

int
XDestroyImage(
XImage *ximage)
{
return((*((ximage)->f.destroy_image))((ximage)));
}


image->f.destroy_image = _XDestroyImage;


static int _XDestroyImage (XImage *ximage)
{
Xfree(ximage->data);
Xfree(ximage->obdata);
Xfree(ximage);
return 1;
}

So yes, the user allocated data gets freed by
XDestroyImage().

br,
KK

Mut...@dastardlyhq.com

unread,
Nov 12, 2023, 11:30:16 AM11/12/23
to
Thanks. Seems a bit inconsistent that XCreateImage() can't allocate but then
thats Xlib for you.

James Kuyper

unread,
Nov 12, 2023, 12:24:27 PM11/12/23
to
In comp.unix.programmer Mut...@dastardlyhq.com wrote:
> I'm a bit uncertain from the man page and from googling around whether
> XDestroyImage() only frees the XImage structure created by XCreateImage()
> or whether it also frees the user allocated memory that gets passed into
> XCreateImage().

The man page that I found describing the XDestroyImage macro says, quite
prominently:

"Note that when the image is created using XCreateImage(), XGetImage(),
or XSubImage(), the destroy procedure that this macro calls frees both
the image structure and the data pointed to by the image structure."

Does the manpage you viewed lack that statement?

Scott Lurndal

unread,
Nov 12, 2023, 12:34:36 PM11/12/23
to
Kalevi answered.

To obtain an answer this question you could have run the application under
valgrind - it would tell you about any memory that hadn't been
deallocated at exit (using the leak check option).

candycanearter07

unread,
Nov 12, 2023, 8:11:56 PM11/12/23
to
Or, you could set the user memory to a stack variable and see what happens.
--
user <candycane> is generated from /dev/urandom

Mut...@dastardlyhq.com

unread,
Nov 13, 2023, 6:05:57 AM11/13/23
to
Yes. All it says is:

"The XDestroyImage function deallocates the memory associated with the
XImage structure."

Which is ambiguous.

Mut...@dastardlyhq.com

unread,
Nov 13, 2023, 6:06:30 AM11/13/23
to
I'd have to install valgrind first. Life's too short.

Scott Lurndal

unread,
Nov 13, 2023, 9:59:24 AM11/13/23
to
sudo apt -y install valgrind
yum install valgrind

not too short for that.

Mut...@dastardlyhq.com

unread,
Nov 13, 2023, 10:57:09 AM11/13/23
to
On Mon, 13 Nov 2023 14:59:22 GMT
fenris$ yum
-bash: yum: command not found

Its a Mac so I'd probably have to piss about with Brew. Like I said, lifes
too short.

Winston

unread,
Nov 13, 2023, 11:53:30 AM11/13/23
to
Check your man page again. Yes, the description of XDestroyImage just
before SEE ALSO says only what you quoted, but, on my libX11 1.8.7
version of the man page, the paragraph James quoted ("Note that ...") is
above, at the 5th paragraph, right after the XCreateImage paragraph.
-WBE

Keith Thompson

unread,
Nov 13, 2023, 2:35:36 PM11/13/23
to
That's very surprising. Looking at a Git mirror of libX11
(https://github.com/mirror/libX11), that "Note that" paragraph appears
in every version of that man page going back to 2003 (X11R6.6).

On my system, Ubuntu 22.04, I get:

$ man XDestroyImage | wc -l
139
$ man XDestroyImage | tail -n 1
X Version 11 libX11 1.7.5 XCreateImage(3)
$ man XDestroyImage | sed -n '/Note that/,+3p'
Note that when the image is created using XCreateImage, XGetImage, or
XSubImage, the destroy procedure that the XDestroyImage function calls
frees both the image structure and the data pointed to by the image
structure.
$

What do you get on your system? (You mentioned that you're on a Mac.)

See also https://linux.die.net/man/3/xdestroyimage

(Note that the XDestroyImage man page is typically a symlink to the
XInitImage man page.)

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Ben Bacarisse

unread,
Nov 15, 2023, 7:32:46 AM11/15/23
to
Just as a random data point, on a Mac I have access to (but don't
understand well enough to say anything reliable about what software it
has installed) I get this:

$ man XDestroyImage | wc -l
140
$ man XDestroyImage | tail -n 1
X Version 11 libX11 1.5.0 XCreateImage(3)
$ man XDestroyImage | sed -n '/Note that/,+3p'
Note that when the image is created using XCreateImage, XGetImage, or
XSubImage, the destroy procedure that the XDestroyImage function calls
frees both the image structure and the data pointed to by the image
structure.

--
Ben.
0 new messages