Discussion:
XDestroyImage()
(too old to reply)
M***@dastardlyhq.com
2023-11-12 15:59:36 UTC
Permalink
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
2023-11-12 16:20:09 UTC
Permalink
Post by M***@dastardlyhq.com
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
M***@dastardlyhq.com
2023-11-12 16:30:14 UTC
Permalink
On Sun, 12 Nov 2023 16:20:09 -0000 (UTC)
Post by Kalevi Kolttonen
Post by M***@dastardlyhq.com
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().
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
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().
Thanks. Seems a bit inconsistent that XCreateImage() can't allocate but then
thats Xlib for you.
James Kuyper
2023-11-12 17:24:25 UTC
Permalink
Post by M***@dastardlyhq.com
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?
M***@dastardlyhq.com
2023-11-13 11:05:55 UTC
Permalink
On Sun, 12 Nov 2023 12:24:25 -0500
Post by James Kuyper
Post by M***@dastardlyhq.com
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
"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?
Yes. All it says is:

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

Which is ambiguous.
Winston
2023-11-13 16:53:54 UTC
Permalink
Post by M***@dastardlyhq.com
On Sun, 12 Nov 2023 12:24:25 -0500
Post by James Kuyper
Post by M***@dastardlyhq.com
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
"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?
"The XDestroyImage function deallocates the memory associated with the
XImage structure."
Which is ambiguous.
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
2023-11-13 19:35:27 UTC
Permalink
Post by M***@dastardlyhq.com
On Sun, 12 Nov 2023 12:24:25 -0500
Post by James Kuyper
Post by M***@dastardlyhq.com
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
"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?
"The XDestroyImage function deallocates the memory associated with the
XImage structure."
Which is ambiguous.
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.Thompson+***@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Ben Bacarisse
2023-11-15 12:32:33 UTC
Permalink
Post by Keith Thompson
Post by M***@dastardlyhq.com
On Sun, 12 Nov 2023 12:24:25 -0500
Post by James Kuyper
Post by M***@dastardlyhq.com
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
"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?
"The XDestroyImage function deallocates the memory associated with the
XImage structure."
Which is ambiguous.
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).
$ 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.)
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.
Scott Lurndal
2023-11-12 17:34:34 UTC
Permalink
Post by M***@dastardlyhq.com
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?
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
2023-11-13 01:11:52 UTC
Permalink
Post by Scott Lurndal
Post by M***@dastardlyhq.com
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?
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).
Or, you could set the user memory to a stack variable and see what happens.
--
user <candycane> is generated from /dev/urandom
M***@dastardlyhq.com
2023-11-13 11:06:28 UTC
Permalink
On Sun, 12 Nov 2023 17:34:34 GMT
Post by Scott Lurndal
Post by M***@dastardlyhq.com
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?
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).
I'd have to install valgrind first. Life's too short.
Scott Lurndal
2023-11-13 14:59:22 UTC
Permalink
Post by M***@dastardlyhq.com
On Sun, 12 Nov 2023 17:34:34 GMT
Post by Scott Lurndal
Post by M***@dastardlyhq.com
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?
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).
I'd have to install valgrind first. Life's too short.
sudo apt -y install valgrind
yum install valgrind

not too short for that.
M***@dastardlyhq.com
2023-11-13 15:57:07 UTC
Permalink
On Mon, 13 Nov 2023 14:59:22 GMT
Post by Scott Lurndal
Post by M***@dastardlyhq.com
On Sun, 12 Nov 2023 17:34:34 GMT
Post by Scott Lurndal
Post by M***@dastardlyhq.com
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?
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).
I'd have to install valgrind first. Life's too short.
sudo apt -y install valgrind
yum install valgrind
not too short for that.
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.
Nicolas George
2023-11-14 16:12:48 UTC
Permalink
Post by M***@dastardlyhq.com
I'd have to install valgrind first. Life's too short.
Life's too short to debug WITHOUT valgrind.
candycanearter07
2023-11-15 17:35:57 UTC
Permalink
Post by Nicolas George
Post by M***@dastardlyhq.com
I'd have to install valgrind first. Life's too short.
Life's too short to debug WITHOUT valgrind.
And gdb is also pretty useful (if you compile with -ggdb).
--
user <candycane> is generated from /dev/urandom
M***@dastardlyhq.com
2023-11-16 08:51:54 UTC
Permalink
On Wed, 15 Nov 2023 11:35:57 -0600
Post by candycanearter07
Post by Nicolas George
Post by M***@dastardlyhq.com
I'd have to install valgrind first. Life's too short.
Life's too short to debug WITHOUT valgrind.
And gdb is also pretty useful (if you compile with -ggdb).
-fsanitize=address is probably the most useful runtime debugging option in
recent times IMO.
candycanearter07
2023-11-20 03:07:42 UTC
Permalink
Post by M***@dastardlyhq.com
On Wed, 15 Nov 2023 11:35:57 -0600
Post by candycanearter07
Post by Nicolas George
Post by M***@dastardlyhq.com
I'd have to install valgrind first. Life's too short.
Life's too short to debug WITHOUT valgrind.
And gdb is also pretty useful (if you compile with -ggdb).
-fsanitize=address is probably the most useful runtime debugging option in
recent times IMO.
True, I keep forgetting to add it tho
--
user <candycane> is generated from /dev/urandom
Loading...