public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [RFA] Fix leaks when pruning inferiors.
@ 2019-12-01 15:52 Philippe Waroquiers
  2019-12-05 16:42 ` Pedro Alves
  2019-12-05 16:44 ` Kevin Buettner
  0 siblings, 2 replies; 4+ messages in thread
From: Philippe Waroquiers @ 2019-12-01 15:52 UTC (permalink / raw)
  To: gdb-patches; +Cc: Philippe Waroquiers

Valgrind detects various inferior related leaks, such as:
  ==31877== 5,530 (56 direct, 5,474 indirect) bytes in 1 blocks are definitely lost in loss record 7,131 of 7,355
  ==31877==    at 0x4C2E18C: calloc (vg_replace_malloc.c:760)
  ==31877==    by 0x23E580: xcalloc (alloc.c:100)
  ==31877==    by 0x4794A9: xcnewvec<void*> (poison.h:158)
  ==31877==    by 0x4794A9: registry_alloc_data(registry_data_registry*, registry_fields*) (registry.c:51)
  ==31877==    by 0x3A537C: inferior_alloc_data (inferior.c:43)
  ==31877==    by 0x3A537C: inferior::inferior(int) (inferior.c:92)
  ==31877==    by 0x3A5426: add_inferior_silent(int) (inferior.c:98)
  ==31877==    by 0x3A5530: add_inferior(int) (inferior.c:122)
  ...

Origin of the leaks is in prune_inferiors: prune_inferiors is first removing
the inferior to prune from the inferior list, then calls delete_inferior.
But delete_inferior will only really destroy the inferior when it finds
it into the inferior list.
As delete_inferior is removing the inferior to delete from the inferior list,
ensure prune_inferiors only calls delete_inferior, without touching the
inferior list.

gdb/ChangeLog
YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
	* inferior.c (prune_inferiors):  Only call delete_inferior,
	Do not modify the inferior list.
---
 gdb/inferior.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 84e4d24e73..55c8409cd9 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -370,24 +370,22 @@ have_live_inferiors (void)
 void
 prune_inferiors (void)
 {
-  struct inferior *ss, **ss_link;
+  struct inferior *ss, *ss_next;
 
   ss = inferior_list;
-  ss_link = &inferior_list;
   while (ss)
     {
       if (!ss->deletable ()
 	  || !ss->removable
 	  || ss->pid != 0)
 	{
-	  ss_link = &ss->next;
-	  ss = *ss_link;
+	  ss = ss->next;
 	  continue;
 	}
 
-      *ss_link = ss->next;
+      ss_next = ss->next;
       delete_inferior (ss);
-      ss = *ss_link;
+      ss = ss_next;
     }
 }
 
-- 
2.20.1

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

* Re: [RFA] Fix leaks when pruning inferiors.
  2019-12-01 15:52 [RFA] Fix leaks when pruning inferiors Philippe Waroquiers
@ 2019-12-05 16:42 ` Pedro Alves
  2019-12-05 22:30   ` Philippe Waroquiers
  2019-12-05 16:44 ` Kevin Buettner
  1 sibling, 1 reply; 4+ messages in thread
From: Pedro Alves @ 2019-12-05 16:42 UTC (permalink / raw)
  To: Philippe Waroquiers, gdb-patches

On 12/1/19 3:52 PM, Philippe Waroquiers wrote:

> Origin of the leaks is in prune_inferiors: prune_inferiors is first removing
> the inferior to prune from the inferior list, then calls delete_inferior.
> But delete_inferior will only really destroy the inferior when it finds
> it into the inferior list.
> As delete_inferior is removing the inferior to delete from the inferior list,
> ensure prune_inferiors only calls delete_inferior, without touching the
> inferior list.

Whoops.

> @@ -370,24 +370,22 @@ have_live_inferiors (void)
>  void
>  prune_inferiors (void)
>  {
> -  struct inferior *ss, **ss_link;
> +  struct inferior *ss, *ss_next;
>  

>  
> -      *ss_link = ss->next;
> +      ss_next = ss->next;
>        delete_inferior (ss);
> -      ss = *ss_link;
> +      ss = ss_next;

Please declare ss_next here instead of at the top.
You can omit the redundant "struct" while at it:

      inferior *next = ss->next;
      delete_inferior (ss);
      ss = next;

OK with that change.  Thanks!

Pedro Alves

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

* Re: [RFA] Fix leaks when pruning inferiors.
  2019-12-01 15:52 [RFA] Fix leaks when pruning inferiors Philippe Waroquiers
  2019-12-05 16:42 ` Pedro Alves
@ 2019-12-05 16:44 ` Kevin Buettner
  1 sibling, 0 replies; 4+ messages in thread
From: Kevin Buettner @ 2019-12-05 16:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Philippe Waroquiers

On Sun,  1 Dec 2019 16:52:20 +0100
Philippe Waroquiers <philippe.waroquiers@skynet.be> wrote:

> Valgrind detects various inferior related leaks, such as:
>   ==31877== 5,530 (56 direct, 5,474 indirect) bytes in 1 blocks are definitely lost in loss record 7,131 of 7,355
>   ==31877==    at 0x4C2E18C: calloc (vg_replace_malloc.c:760)
>   ==31877==    by 0x23E580: xcalloc (alloc.c:100)
>   ==31877==    by 0x4794A9: xcnewvec<void*> (poison.h:158)
>   ==31877==    by 0x4794A9: registry_alloc_data(registry_data_registry*, registry_fields*) (registry.c:51)
>   ==31877==    by 0x3A537C: inferior_alloc_data (inferior.c:43)
>   ==31877==    by 0x3A537C: inferior::inferior(int) (inferior.c:92)
>   ==31877==    by 0x3A5426: add_inferior_silent(int) (inferior.c:98)
>   ==31877==    by 0x3A5530: add_inferior(int) (inferior.c:122)
>   ...
> 
> Origin of the leaks is in prune_inferiors: prune_inferiors is first removing
> the inferior to prune from the inferior list, then calls delete_inferior.
> But delete_inferior will only really destroy the inferior when it finds
> it into the inferior list.
> As delete_inferior is removing the inferior to delete from the inferior list,
> ensure prune_inferiors only calls delete_inferior, without touching the
> inferior list.
> 
> gdb/ChangeLog
> YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
> 	* inferior.c (prune_inferiors):  Only call delete_inferior,
> 	Do not modify the inferior list.

Okay.

(Though I'd rather see a semicolon or a period in place of the comma in
the ChangeLog entry.)

Kevin

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

* Re: [RFA] Fix leaks when pruning inferiors.
  2019-12-05 16:42 ` Pedro Alves
@ 2019-12-05 22:30   ` Philippe Waroquiers
  0 siblings, 0 replies; 4+ messages in thread
From: Philippe Waroquiers @ 2019-12-05 22:30 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On Thu, 2019-12-05 at 16:42 +0000, Pedro Alves wrote:
> Please declare ss_next here instead of at the top.
> You can omit the redundant "struct" while at it:
> 
>       inferior *next = ss->next;
>       delete_inferior (ss);
>       ss = next;
> 
> OK with that change.  Thanks!
Pushed after fixing the above,
and the ChangeLog message as suggested by Kevin.

Thanks for the review.

Philippe


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

end of thread, other threads:[~2019-12-05 22:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-01 15:52 [RFA] Fix leaks when pruning inferiors Philippe Waroquiers
2019-12-05 16:42 ` Pedro Alves
2019-12-05 22:30   ` Philippe Waroquiers
2019-12-05 16:44 ` Kevin Buettner

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).