Bug 121972 - [PDT] pdt_70/77/86.f03 leak memory
Summary: [PDT] pdt_70/77/86.f03 leak memory
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 16.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: PDT
  Show dependency tree / graph
 
Reported: 2025-09-17 11:11 UTC by Paul Thomas
Modified: 2026-07-01 20:43 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-09-17 00:00:00


Attachments
This patch plugs the memory leak in pdt_77.f03 (559 bytes, patch)
2026-06-17 09:36 UTC, Paul Thomas
Details | Diff
Fix for leakage in pdt_70.f03 (1.44 KB, patch)
2026-06-19 15:55 UTC, Jerry DeLisle
Details | Diff
Alternative patch for the leak in pdt_70.f03 (1.76 KB, application/mbox)
2026-06-22 12:04 UTC, Paul Thomas
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Thomas 2025-09-17 11:11:18 UTC
==1367152== Memcheck, a memory error detector
==1367152== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==1367152== Using Valgrind-3.25.1 and LibVEX; rerun with -h for copyright info
==1367152== Command: ./a.out
==1367152== 
==1367152== 
==1367152== HEAP SUMMARY:
==1367152==     in use at exit: 128 bytes in 2 blocks
==1367152==   total heap usage: 32 allocs, 30 frees, 1,317,852 bytes allocated
==1367152== 
==1367152== 64 bytes in 1 blocks are definitely lost in loss record 1 of 2
==1367152==    at 0x483EB26: malloc (vg_replace_malloc.c:446)
==1367152==    by 0x401196: MAIN__ (pdt_3.f03:57)
==1367152==    by 0x402157: main (pdt_3.f03:11)

57  matrix = w%d ! leak goes away if 'matrix' deallocated at end of program.

==1367152== 
==1367152== 64 bytes in 1 blocks are definitely lost in loss record 2 of 2
==1367152==    at 0x483EB26: malloc (vg_replace_malloc.c:446)
==1367152==    by 0x401344: MAIN__ (pdt_3.f03:60)
==1367152==    by 0x402157: main (pdt_3.f03:11)

60  allocate (cz, source = mytype(ftype, d_dim)( 0, matrix))

==1367152== 
==1367152== LEAK SUMMARY:
==1367152==    definitely lost: 128 bytes in 2 blocks
==1367152==    indirectly lost: 0 bytes in 0 blocks
==1367152==      possibly lost: 0 bytes in 0 blocks
==1367152==    still reachable: 0 bytes in 0 blocks
==1367152==         suppressed: 0 bytes in 0 blocks
==1367152== 
==1367152== For lists of detected and suppressed errors, rerun with: -s
==1367152== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Comment 1 Paul Thomas 2025-09-17 11:21:01 UTC
        cz._vptr->_copy (&source.27, cz._data);

without 'source.27' being freed afterwards.

Paul
Comment 2 Paul Thomas 2025-12-12 17:14:31 UTC
Out of 79 tests gfortran.dg/pdt*.f*, the following leak memory:

pdt_2.f03 - should fail in runtime and so leaks memory.

pdt_3.f03, pdt_39.f03 - sources of memory leaks are marked as comments

pdt_70.f03 - fails with memory allocated from line 108: call alloc_ts(c) 

pdt_77.f03 - not investigated in detail yet.

A common theme appears to leaks caused by allocations of PDT with source.

Paul
Comment 3 Paul Thomas 2025-12-12 17:15:02 UTC
Out of 79 tests gfortran.dg/pdt*.f*, the following leak memory:

pdt_2.f03 - should fail in runtime and so leaks memory.

pdt_3.f03, pdt_39.f03 - sources of memory leaks are marked as comments

pdt_70.f03 - fails with memory allocated from line 108: call alloc_ts(c) 

pdt_77.f03 - not investigated in detail yet.

A common theme appears to leaks caused by allocations of PDT with source.

Paul
Comment 4 Paul Thomas 2026-01-07 16:35:42 UTC
pdt_3.f03 is struck off the list with the fix for PR90218. Changing the subject line accordingly.

Paul
Comment 5 Paul Thomas 2026-03-10 16:22:20 UTC
pdt_39.f03 removed from list but pdt_86.f03 added.

Paul
Comment 6 Paul Thomas 2026-06-17 09:36:32 UTC
Created attachment 64753 [details]
This patch plugs the memory leak in pdt_77.f03

Remaining leaky pdt tests are:

pdt_70/pdt_80/pdt_82/pdt_83/pdt_86.f03 and pdt_array_alloc_1.f90.

I will do some investigating before submitting the attached to the list.

Paul
Comment 7 Jerry DeLisle 2026-06-19 15:55:06 UTC
Created attachment 64803 [details]
Fix for leakage in pdt_70.f03

This one fixes one of the leaks.
Comment 8 Paul Thomas 2026-06-22 12:04:19 UTC
Created attachment 64825 [details]
Alternative patch for the leak in pdt_70.f03

Hi Jerry,

I dropped your or Claude's testcase to make the patch free of any possible copyright violation. Instead, pdt_70.f03 has been extended slightly to both do the original test and check this fix.

With that, I am OK with a push to mainline and a later backport.

Regards

Paul
Comment 9 GCC Commits 2026-06-23 08:35:22 UTC
The master branch has been updated by Paul Thomas <pault@gcc.gnu.org>:

https://gcc.gnu.org/g:bb873ada17b5cea56e7ac4c4156af2f07e8db2cf

commit r17-1767-gbb873ada17b5cea56e7ac4c4156af2f07e8db2cf
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Tue Jun 23 10:21:47 2026 +0100

    Fortran: Fix memory leaks in PDT tests. [PR121972]
    
    2026-06-22  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/121972
            * decl.cc (build_struct): Convert the PDT template before the
            call to gfc_build_class_symbol.
    
    gcc/testsuite/
            PR fortran/121972
            * gfortran.dg/pdt_77.f03: Add 'finalalize_navier_stokes', which
            tests that the 'navier_stokes' component 'p' is pointed to the
            correct vtable in both tests. Check that VIEW_CONVERT_EXPR does
            not appear in the tree dump.
Comment 10 GCC Commits 2026-06-23 19:25:22 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:9579e3eb64ec89c4c0781bb0a59409f15c997213

commit r17-1786-g9579e3eb64ec89c4c0781bb0a59409f15c997213
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date:   Fri Jun 19 14:54:00 2026 -0700

    fortran: Fix memory leak for array structure-constructor element [PR121972]
    
    Fix the remaining leak in pdt_86.f03. Added a new test in asan to avoid it.
    
            PR fortran/121972
    
    gcc/fortran/ChangeLog:
    
            * trans-array.cc (has_class_alloc_comp): New helper; returns true if
            derived type DER has any CLASS component.
            (gfc_trans_array_ctor_element): Also free allocatable components when
            the element expression is EXPR_STRUCTURE, skipping types that contain
            CLASS components to avoid freeing stack-allocated _data pointers.
            (gfc_constructor_is_owned_alloc_comp): Likewise treat EXPR_STRUCTURE
            elements as owned only when the derived type has no CLASS components.
            * trans-expr.cc (gfc_trans_alloc_subarray_assign): Free allocatable
            components of the component's temporary descriptor before nulling its
            data pointer, for non-variable source expressions.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/class_array_15.f03: Adjust expected free count.
            * gfortran.dg/derived_constructor_comps_6.f90: Adjust expected free count.
            * gfortran.dg/asan/structure_constructor_alloc_comp_leak_1.f90: New test.
Comment 11 GCC Commits 2026-06-26 04:01:39 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:039e2254d55e5d53f2766ead3495413b4788e00c

commit r17-1909-g039e2254d55e5d53f2766ead3495413b4788e00c
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date:   Thu Jun 25 20:54:48 2026 -0700

    fortran: [PR121972] Fix attribute used.
    
            PR fortran/121972
    
    gcc/fortran/ChangeLog:
    
            * trans-array.cc (has_class_alloc_comp): Change to atte.class_pointer.
Comment 12 GCC Commits 2026-06-30 15:19:43 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:836f029d6a0ee0e6bd88605da994681498f5cf6a

commit r17-2025-g836f029d6a0ee0e6bd88605da994681498f5cf6a
Author: Jerry DeLisle <jvdelisle@gcc.gnu.org>
Date:   Mon Jun 29 08:36:06 2026 -0700

    fortran: Fix finalizer list truncated when >= 3 finalizers match [PR121972]
    
    When gfc_resolve_finalizers copies matching finalizers from the template
    type into a derived type's finalizer list, a linked-list bug caused the
    third and later entries to be orphaned.  Fix by using the standard
    tail-pointer idiom.
    
            PR fortran/121972
    
    gcc/fortran/ChangeLog:
    
            * resolve.cc (gfc_resolve_finalizers): Fix linked-list tail-pointer
            bug that dropped all but the first two finalizers from a derived
            type's finalizer list when three or more matched.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pdt_70.f03: Add a matrix finalizer and update the
            check of the value of 'flag'.
Comment 13 GCC Commits 2026-06-30 17:33:39 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:1a2de01fbac64f97fed6f63ff655b13bff831877

commit r17-2028-g1a2de01fbac64f97fed6f63ff655b13bff831877
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Tue Jun 30 10:39:57 2026 +0100

    Fortran: Fix memory leak found in pdt_86.f03 [PR121972]
    
    Followup on memory leaks found using -fsanitize=address
    in existing test cases.
    
    Co-authored-by: Jerry DeLisle <jvdelisle@gcc.gnu.org>
    
            PR fortran/121972
    
    gcc/fortran/
    
            * decl.cc (gfc_get_pdt_instance): A PDT that has a derived type
            component, which has allocatable components, must be marked as
            having allocatable components.
            * resolve.cc (gfc_resolve_ref): Initialize last_pdt from the base
            symbol's declared type when the expression type is not a PDT.
    
    gcc/testsuite/
    
            * gfortran.dg/pdt_86.f03: Add tree dump for counts of frees and
            mallocs (41 & 31 respectively).
            * gfortran.dg/pdt_86_reduced.f90: New test to check that the
            non-PDT version of the leak has gone.
Comment 14 GCC Commits 2026-07-01 20:43:28 UTC
The master branch has been updated by Jerry DeLisle <jvdelisle@gcc.gnu.org>:

https://gcc.gnu.org/g:95dbc32189ab0433283f337b4b490bfcec0bd475

commit r17-2071-g95dbc32189ab0433283f337b4b490bfcec0bd475
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Wed Jul 1 17:14:25 2026 +0100

    Fortran: Fix asan problems with PDT testcases [PR121972]
    
    Co-authored-by: Jerry DeLisle <jvdelisle@gcc.gnu.org>
    
            PR fortran/121972
    
    gcc/fortran
            * expr.cc (has_parameterized_comps): Return false if the DT
            is neither a pdt_type nor has PDT components. Correct the
            logic for a PDT component.
            * trans-expr.cc (alloc_scalar_allocatable_for_assignment): Use
            calloc for types with paramterized components as well as those
            with allocatable components.
            * trans-stmt.cc (gfc_trans_allocate): Merge the allocation of
            parameterized components of PDTs and class PDTs into one block
            If an allocate type_spec is present that has allocatable comps
            where the class declared type does not, nullify the allocatable
            components.
    
    gcc/testsuite/
            * gfortran.dg/asan/pdt_46.f03: Copy of original with tree dump
            for counts of frees, mallocs and callocs removed.
            * gfortran.dg/asan/pdt_77.f03: Ditto.
            * gfortran.dg/pdt_46.f03: Calloc count added, corresponding to
            reduction in mallocs..
            * gfortran.dg/pdt_50.f03: Ditto.