unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
@ 2019-04-07  5:16 Alex
  2019-04-07  6:37 ` Eli Zaretskii
  0 siblings, 1 reply; 8+ messages in thread
From: Alex @ 2019-04-07  5:16 UTC (permalink / raw)
  To: 35179

[-- Attachment #1: Type: text/plain, Size: 132 bytes --]

Also declare `name' in MonitorInfo const to satisfy the compiler.

Is it too late to push a memory leak fix like this to emacs-26?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: GTK memory leak --]
[-- Type: text/x-patch, Size: 1726 bytes --]

From b9c71187840f72f5145da5794e1ca69d4a074654 Mon Sep 17 00:00:00 2001
From: Alexander Gramiak <agrambot@gmail.com>
Date: Sat, 6 Apr 2019 23:02:24 -0600
Subject: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list

* src/xfns.c (x-display-monitor-attributes-list): Plug memory leak and
  don't needlessly duplicate the model name.

* src/frame.h (MonitorInfo): Declare name as pointing to const char.
---
 src/frame.h | 2 +-
 src/xfns.c  | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/frame.h b/src/frame.h
index ed62e7ace0..b1eedf36a3 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -1648,7 +1648,7 @@ flush_frame (struct frame *f)
 struct MonitorInfo {
   XRectangle geom, work;
   int mm_width, mm_height;
-  char *name;
+  const char *name;
 };
 
 extern void free_monitors (struct MonitorInfo *monitors, int n_monitors);
diff --git a/src/xfns.c b/src/xfns.c
index f238a3daa1..396e77c1db 100644
--- a/src/xfns.c
+++ b/src/xfns.c
@@ -5030,7 +5030,7 @@ Internal use only, use `display-monitor-attributes-list' instead.  */)
       mi->mm_height = height_mm;
 
 #if GTK_CHECK_VERSION (3, 22, 0)
-      mi->name = g_strdup (gdk_monitor_get_model (monitor));
+      mi->name = gdk_monitor_get_model (monitor);
 #elif GTK_CHECK_VERSION (2, 14, 0)
       mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
 #endif
@@ -5041,6 +5041,7 @@ Internal use only, use `display-monitor-attributes-list' instead.  */)
                                                  primary_monitor,
                                                  monitor_frames,
                                                  source);
+  xfree (monitors);
   unblock_input ();
 #else  /* not USE_GTK */
 
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07  5:16 bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list Alex
@ 2019-04-07  6:37 ` Eli Zaretskii
  2019-04-07 14:51   ` Alex
  0 siblings, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2019-04-07  6:37 UTC (permalink / raw)
  To: 35179, agrambot

On April 7, 2019 8:16:53 AM GMT+03:00, Alex <agrambot@gmail.com> wrote:
> Also declare `name' in MonitorInfo const to satisfy the compiler.
> 
> Is it too late to push a memory leak fix like this to emacs-26?

Yes, it's too late for such non-trivial changes in Emacs 26.2.

But I have a more fundamental problem with your proposed patch: it looks like you are relying on implementation details of gdk_monitor_get_model that its documentation never advertises?  Otherwise, how did you know that just removing the g_strdup call will plug a memory leak here, and will not create any new problems?

I'd prefer to leave g_strdup intact, and instead explicitly release the storage of previous value.  IOW, plug the leak in our own code, not rely on undocumented features which can easily go away some day.

Thanks.






^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07  6:37 ` Eli Zaretskii
@ 2019-04-07 14:51   ` Alex
  2019-04-07 16:32     ` Eli Zaretskii
  0 siblings, 1 reply; 8+ messages in thread
From: Alex @ 2019-04-07 14:51 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 35179

Eli Zaretskii <eliz@gnu.org> writes:

> But I have a more fundamental problem with your proposed patch: it looks like
> you are relying on implementation details of gdk_monitor_get_model that its
> documentation never advertises? Otherwise, how did you know that just removing
> the g_strdup call will plug a memory leak here, and will not create any new
> problems?
>
> I'd prefer to leave g_strdup intact, and instead explicitly release the storage of previous value.  IOW, plug the leak in our own code, not rely on undocumented features which can easily go away some day.

The documentation of gdk_monitor_get_model[1] specifies that the return
value is "[transfer none]", which has the description "Don't free data
after code is done".

The main memory leak, though, was that the MonitorList array wasn't
being freed. I considered using the free_monitors procedure like the
non-GTK versions do, but I saw no reason to call g_strdup for each name
and free each name almost right after.

Since make_monitor_attribute_list uses make_string on each name, I don't
see any issues that this removal would cause.


[1] https://developer.gnome.org/gdk3/stable/GdkMonitor.html#gdk-monitor-get-model





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07 14:51   ` Alex
@ 2019-04-07 16:32     ` Eli Zaretskii
  2019-04-07 17:34       ` Alex
  0 siblings, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2019-04-07 16:32 UTC (permalink / raw)
  To: Alex; +Cc: 35179

> From: Alex <agrambot@gmail.com>
> Cc: bug-gnu-emacs@gnu.org, 35179@debbugs.gnu.org
> Date: Sun, 07 Apr 2019 08:51:12 -0600
> 
> > I'd prefer to leave g_strdup intact, and instead explicitly release the storage of previous value.  IOW, plug the leak in our own code, not rely on undocumented features which can easily go away some day.
> 
> The documentation of gdk_monitor_get_model[1] specifies that the return
> value is "[transfer none]", which has the description "Don't free data
> after code is done".

That could mean anything.  The only thing it tells us not to call
'free' on the result, but it could be, for example, that the result is
a pointer to a static buffer that can be changed by a subsequent call
to the function.

> The main memory leak, though, was that the MonitorList array wasn't
> being freed.

Does your patch change that?  If not, why not?

> I considered using the free_monitors procedure like the non-GTK
> versions do, but I saw no reason to call g_strdup for each name and
> free each name almost right after.

I don't see how the short lifetime of the array changes anything
here.  As long as we aren't sure the pointer returned by
gdk_monitor_get_model is a copy that cannot be changed by another
thread, we should ourselves make a copy.  Otherwise, who can ensure us
that some other GTK thread doesn't call this same function during the
short life time of the array?

> Since make_monitor_attribute_list uses make_string on each name, I don't
> see any issues that this removal would cause.

The issue I see is that between the time we call gdk_monitor_get_model
and the time we use the string something could change the string to
which the pointer points.  If you can spot something in the GDK docs
that guarantees this couldn't happen, please point me to that piece of
docs.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07 16:32     ` Eli Zaretskii
@ 2019-04-07 17:34       ` Alex
  2019-04-07 17:44         ` Alex
  0 siblings, 1 reply; 8+ messages in thread
From: Alex @ 2019-04-07 17:34 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 35179

[-- Attachment #1: Type: text/plain, Size: 1553 bytes --]

Eli Zaretskii <eliz@gnu.org> writes:

>> The main memory leak, though, was that the MonitorList array wasn't
>> being freed.
>
> Does your patch change that?  If not, why not?

It did, yes. That was the xfree (monitors) call.

>> I considered using the free_monitors procedure like the non-GTK
>> versions do, but I saw no reason to call g_strdup for each name and
>> free each name almost right after.
>
> I don't see how the short lifetime of the array changes anything
> here.  As long as we aren't sure the pointer returned by
> gdk_monitor_get_model is a copy that cannot be changed by another
> thread, we should ourselves make a copy.  Otherwise, who can ensure us
> that some other GTK thread doesn't call this same function during the
> short life time of the array?

The documentation does state that the name property of the monitor is
read-only.

>> Since make_monitor_attribute_list uses make_string on each name, I don't
>> see any issues that this removal would cause.
>
> The issue I see is that between the time we call gdk_monitor_get_model
> and the time we use the string something could change the string to
> which the pointer points.  If you can spot something in the GDK docs
> that guarantees this couldn't happen, please point me to that piece of
> docs.

Well, I suppose that the monitor could be unplugged in-between, which
presumably would mean that the monitor object is freed. Would that space
be reused in-between, though?

I suppose it doesn't hurt to play it safe, so I updated the patch to use
free_monitors instead:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2 --]
[-- Type: text/x-patch, Size: 1931 bytes --]

From 490ec531943540cb882b46624a4b7b3b1463a4b8 Mon Sep 17 00:00:00 2001
From: Alexander Gramiak <agrambot@gmail.com>
Date: Sat, 6 Apr 2019 23:02:24 -0600
Subject: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list

* src/frame.c (free_monitors) [USE_GTK]: Define in the GTK case as
  well.

* src/xfns.c (x-display-monitor-attributes-list) [USE_GTK]: Plug
  memory leak.

* src/frame.h (MonitorInfo): Declare name as pointing to const char.
---
 src/frame.c | 4 ++--
 src/frame.h | 2 +-
 src/xfns.c  | 1 +
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/frame.c b/src/frame.c
index d0c77149ba..e57f17ded4 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -5662,8 +5662,8 @@ selected frame.  This is useful when `make-pointer-invisible' is set.  */)
 
 #ifdef HAVE_WINDOW_SYSTEM
 
-# if (defined HAVE_NS \
-      || (!defined USE_GTK && (defined HAVE_XINERAMA || defined HAVE_XRANDR)))
+# if (defined USE_GTK || defined HAVE_NS || defined HAVE_XINERAMA \
+  || defined HAVE_XRANDR
 void
 free_monitors (struct MonitorInfo *monitors, int n_monitors)
 {
diff --git a/src/frame.h b/src/frame.h
index ed62e7ace0..b1eedf36a3 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -1648,7 +1648,7 @@ flush_frame (struct frame *f)
 struct MonitorInfo {
   XRectangle geom, work;
   int mm_width, mm_height;
-  char *name;
+  const char *name;
 };
 
 extern void free_monitors (struct MonitorInfo *monitors, int n_monitors);
diff --git a/src/xfns.c b/src/xfns.c
index f238a3daa1..33595afc1e 100644
--- a/src/xfns.c
+++ b/src/xfns.c
@@ -5041,6 +5041,7 @@ Internal use only, use `display-monitor-attributes-list' instead.  */)
                                                  primary_monitor,
                                                  monitor_frames,
                                                  source);
+  free_monitors (monitors, n_monitors);
   unblock_input ();
 #else  /* not USE_GTK */
 
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07 17:34       ` Alex
@ 2019-04-07 17:44         ` Alex
  2019-04-07 18:21           ` Eli Zaretskii
  0 siblings, 1 reply; 8+ messages in thread
From: Alex @ 2019-04-07 17:44 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 35179

[-- Attachment #1: Type: text/plain, Size: 50 bytes --]

Whoops, I forgot to add a closing parenthesis...


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2.000001 --]
[-- Type: text/x-patch, Size: 1936 bytes --]

From eca698eaf5404071e90505d195c5237739f244db Mon Sep 17 00:00:00 2001
From: Alexander Gramiak <agrambot@gmail.com>
Date: Sat, 6 Apr 2019 23:02:24 -0600
Subject: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list

* src/frame.c (free_monitors) [USE_GTK]: Define in the GTK case as
  well.

* src/xfns.c (x-display-monitor-attributes-list) [USE_GTK]: Plug
  memory leak.

* src/frame.h (MonitorInfo): Declare name as pointing to const char.
---
 src/frame.c | 4 ++--
 src/frame.h | 2 +-
 src/xfns.c  | 1 +
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/frame.c b/src/frame.c
index d0c77149ba..6fdb7d0cbb 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -5662,8 +5662,8 @@ selected frame.  This is useful when `make-pointer-invisible' is set.  */)
 
 #ifdef HAVE_WINDOW_SYSTEM
 
-# if (defined HAVE_NS \
-      || (!defined USE_GTK && (defined HAVE_XINERAMA || defined HAVE_XRANDR)))
+# if (defined USE_GTK || defined HAVE_NS || defined HAVE_XINERAMA \
+      || defined HAVE_XRANDR)
 void
 free_monitors (struct MonitorInfo *monitors, int n_monitors)
 {
diff --git a/src/frame.h b/src/frame.h
index ed62e7ace0..b1eedf36a3 100644
--- a/src/frame.h
+++ b/src/frame.h
@@ -1648,7 +1648,7 @@ flush_frame (struct frame *f)
 struct MonitorInfo {
   XRectangle geom, work;
   int mm_width, mm_height;
-  char *name;
+  const char *name;
 };
 
 extern void free_monitors (struct MonitorInfo *monitors, int n_monitors);
diff --git a/src/xfns.c b/src/xfns.c
index f238a3daa1..33595afc1e 100644
--- a/src/xfns.c
+++ b/src/xfns.c
@@ -5041,6 +5041,7 @@ Internal use only, use `display-monitor-attributes-list' instead.  */)
                                                  primary_monitor,
                                                  monitor_frames,
                                                  source);
+  free_monitors (monitors, n_monitors);
   unblock_input ();
 #else  /* not USE_GTK */
 
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07 17:44         ` Alex
@ 2019-04-07 18:21           ` Eli Zaretskii
  2019-04-07 18:52             ` Alex
  0 siblings, 1 reply; 8+ messages in thread
From: Eli Zaretskii @ 2019-04-07 18:21 UTC (permalink / raw)
  To: Alex; +Cc: 35179

> From: Alex <agrambot@gmail.com>
> Cc: 35179@debbugs.gnu.org
> Date: Sun, 07 Apr 2019 11:44:54 -0600
> 
> Whoops, I forgot to add a closing parenthesis...

Thanks.  This variant is fine with me, but since we use xfree to free
the name, I think we should use xstrdup, not g_strdup, to copy it, as
the latter is documented to need g_free to free the storage.





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list
  2019-04-07 18:21           ` Eli Zaretskii
@ 2019-04-07 18:52             ` Alex
  0 siblings, 0 replies; 8+ messages in thread
From: Alex @ 2019-04-07 18:52 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 35179

close 35179
quit

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Alex <agrambot@gmail.com>
>> Cc: 35179@debbugs.gnu.org
>> Date: Sun, 07 Apr 2019 11:44:54 -0600
>> 
>> Whoops, I forgot to add a closing parenthesis...
>
> Thanks.  This variant is fine with me, but since we use xfree to free
> the name, I think we should use xstrdup, not g_strdup, to copy it, as
> the latter is documented to need g_free to free the storage.

Okay, I pushed it as a35e06bbe2. I also added in a GTK version check
since `name' isn't used if !GTK_CHECK_VERSION(2, 14, 0).





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-04-07 18:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-07  5:16 bug#35179: [PATCH] Plug memory leak in GTK x-display-monitor-attributes-list Alex
2019-04-07  6:37 ` Eli Zaretskii
2019-04-07 14:51   ` Alex
2019-04-07 16:32     ` Eli Zaretskii
2019-04-07 17:34       ` Alex
2019-04-07 17:44         ` Alex
2019-04-07 18:21           ` Eli Zaretskii
2019-04-07 18:52             ` Alex

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).