Dear All,

the attached obvious patch fixes frontend memleaks for trivial code such
as

  program p
    real, pointer :: x(:)
    allocate (x, source = reshape ([1.], [1]))
  end

which showed 4(!) references to __gmp_default_allocate when running f951
under valgrind.  For ALLOCATE, component expr3 of the SOURCE expression
was the culprit.

Regtested clean on x86_64-pc-linux-gnu.

Pushed as r16-4013-g518ec01b363ba7 .

Thanks,
Harald

From 518ec01b363ba782fe9a9b09a05a2716cedf8b01 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Sun, 21 Sep 2025 21:54:44 +0200
Subject: [PATCH] Fortran: fix frontend memory leaks for ALLOCATE with SOURCE
 [PR109010]

When running under valgrind the Fortran frontend showed several leaks
involving __gmp_default_allocate for simple code such as

  program p
    real, pointer :: x(:)
    allocate (x, source = reshape ([1.], [1]))
  end

as not all used components of the structure gfc_code were freed when a
statement was freed.  Just do so.

	PR fortran/109010

gcc/fortran/ChangeLog:

	* st.cc (gfc_free_statement): Also free components expr3 and expr4.
---
 gcc/fortran/st.cc | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/gcc/fortran/st.cc b/gcc/fortran/st.cc
index f7f67b18b67..2281bf6e03b 100644
--- a/gcc/fortran/st.cc
+++ b/gcc/fortran/st.cc
@@ -86,6 +86,10 @@ gfc_free_statement (gfc_code *p)
     gfc_free_expr (p->expr1);
   if (p->expr2)
     gfc_free_expr (p->expr2);
+  if (p->expr3)
+    gfc_free_expr (p->expr3);
+  if (p->expr4)
+    gfc_free_expr (p->expr4);
 
   switch (p->op)
     {
-- 
2.51.0

Reply via email to