public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Costas Argyris <costas.argyris@gmail.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Jonathan Wakely <jwakely@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] driver: Fix memory leak.
Date: Thu, 7 Dec 2023 14:28:18 +0000	[thread overview]
Message-ID: <CAHyHGCnNb-B78c+gWaq4jsLcANVWAFj87L3Xx_5D__-JkY1NPA@mail.gmail.com> (raw)
In-Reply-To: <ZXCHrFNXe+YvEJX4@tucnak>


[-- Attachment #1.1: Type: text/plain, Size: 1379 bytes --]

Would that be something like this?

Although it didn't fix the leak, which was the entire point of this
exercise.

Maybe because driver::finalize () is not getting called so the call to
mdswitches.release () doesn't really happen, which was the reason
I went with std::vector in the first place because it takes care of itself.


On Wed, 6 Dec 2023 at 14:39, Jakub Jelinek <jakub@redhat.com> wrote:

> On Wed, Dec 06, 2023 at 02:29:25PM +0000, Costas Argyris wrote:
> > Attached a new patch with these changes.
> >
> > On Mon, 4 Dec 2023 at 12:15, Jonathan Wakely <jwakely@redhat.com> wrote:
> >
> > > On Sat, 2 Dec 2023 at 21:24, Costas Argyris wrote:
> > > >
> > > > Use std::vector instead of malloc'd pointer
> > > > to get automatic freeing of memory.
> > >
> > > You can't include <vector> there. Instead you need to define
> > > INCLUDE_VECTOR before "system.h"
> > >
> > > Shouldn't you be using resize, not reserve? Otherwise mdswitches[i] is
> > > undefined.
>
> Any reason not to use vec.h instead?
> I especially don't like the fact that with a global
> std::vector<whatever> var;
> it means runtime __cxa_atexit for the var the destruction, which it really
> doesn't need on exit.
>
> We really don't need to free the memory at exit time, that is just wasted
> cycles, all we need is that it is freed before the pointer or vector is
> cleared.
>
>         Jakub
>
>

[-- Attachment #2: 0001-driver-Fix-memory-leak.patch --]
[-- Type: application/octet-stream, Size: 1796 bytes --]

From a76942fcacd12ad95667cc782c8d0aff23ceafab Mon Sep 17 00:00:00 2001
From: Costas Argyris <costas.argyris@gmail.com>
Date: Sat, 2 Dec 2023 20:52:07 +0000
Subject: [PATCH] driver: Fix memory leak.

Use vec instead of malloc'd pointer
to get automatic freeing of memory.

Result was verified by valgrind, which showed
one less loss record.

Signed-off-by: Costas Argyris <costas.argyris@gmail.com>
---
 gcc/gcc.cc | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 9f21ad9453e..6534e09860c 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -9534,7 +9534,7 @@ struct mdswitchstr
   int len;
 };
 
-static struct mdswitchstr *mdswitches;
+static vec<struct mdswitchstr> mdswitches;
 static int n_mdswitches;
 
 /* Check whether a particular argument was used.  The first time we
@@ -9751,9 +9751,7 @@ set_multilib_dir (void)
 
   if (n_mdswitches)
     {
-      int i = 0;
-
-      mdswitches = XNEWVEC (struct mdswitchstr, n_mdswitches);
+      mdswitches.create (n_mdswitches);
       for (start = multilib_defaults; *start != '\0'; start = end + 1)
 	{
 	  while (*start == ' ' || *start == '\t')
@@ -9768,8 +9766,10 @@ set_multilib_dir (void)
 
 	  obstack_grow (&multilib_obstack, start, end - start);
 	  obstack_1grow (&multilib_obstack, 0);
-	  mdswitches[i].str = XOBFINISH (&multilib_obstack, const char *);
-	  mdswitches[i++].len = end - start;
+	  struct mdswitchstr tmp;
+	  tmp.str = XOBFINISH (&multilib_obstack, const char *);
+	  tmp.len = end - start;
+	  mdswitches.safe_push (tmp);
 
 	  if (*end == '\0')
 	    break;
@@ -11366,7 +11366,7 @@ driver::finalize ()
   input_from_pipe = 0;
   suffix_subst = NULL;
 
-  mdswitches = NULL;
+  mdswitches.release ();
   n_mdswitches = 0;
 
   used_arg.finalize ();
-- 
2.30.2


  reply	other threads:[~2023-12-07 14:28 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-02 21:24 Costas Argyris
2023-12-04 12:15 ` Jonathan Wakely
2023-12-06 14:29   ` Costas Argyris
2023-12-06 14:39     ` Jakub Jelinek
2023-12-07 14:28       ` Costas Argyris [this message]
2023-12-07 14:42         ` Jakub Jelinek
2023-12-07 15:16           ` Costas Argyris
2023-12-07 15:42             ` Jakub Jelinek
2023-12-07 16:01               ` Costas Argyris
2023-12-07 16:04                 ` Jakub Jelinek
2023-12-08 12:18                   ` Costas Argyris
2023-12-08 13:16                     ` Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHyHGCnNb-B78c+gWaq4jsLcANVWAFj87L3Xx_5D__-JkY1NPA@mail.gmail.com \
    --to=costas.argyris@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jwakely@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).