Re: [Patch, fortran] PR121972 - [PDT] Fix for the pdt_86 leak.
Paul Richard Thomas <[email protected]> Tue, 23 Jun 2026 11:00:59 +0100
| Newsgroups | gmane.comp.gcc.patches,gmane.comp.gcc.fortran |
|---|---|
| Message-ID | <CAGkQGiJXRgTYCa3P7CO1fneiuMnVbY+CmLGut3LBMRj0EhsUNw@mail.gmail.com> |
Hello Jerry and Harald, The patch does what it says on the box and regression tests correctly. Concerning, Haralds suggestion: I don't see any problem in principle, as long as it is recognised that both calls to has_class_alloc_comp are negated and so types with class pointer components get through the gate. gfc_deallocate_alloc_comp_no_caf should ignore them but I haven't checked. In chunks 2 and 3, please move expr->ts.type == BT_DERIVED to the start of the condition to guard the use of the typespec derived field in the call to has_class_alloc_comp. It should be OK as it is but it is worthwhile taking that extra little bit of care :-) Finally, the leaking part of pdt_86.f03 has been isolated as an additional test. If you think that it is useful, please add it to the patch. (Renamed to pdt_86_leak_test.f90?) OK for mainline and backporting. Cheers Paul On Tue, 23 Jun 2026 at 00:05, Jerry D <[email protected]> wrote: > > On 6/22/26 1:35 PM, Harald Anlauf wrote: > > Hi Jerry! > > > > Am 22.06.26 um 8:37 PM schrieb Jerry D: > >> See attached patch. > >> > >> Regression tested on x86_64. > >> > >> A new test case is added to asan to catch this in the future. > > > > I'd rather leave the review to Paul, but I have one question: > > As is, the new helper has_class_alloc_comp might return true for > > pointer components, but I think it should not. > > > > Can you please check? > > > > Harald, I think you are right. I changed it to this: > > static bool > has_class_alloc_comp (gfc_symbol *der) > { > for (gfc_component *c = der->components; c; c = c->next) > if (c->ts.type == BT_CLASS && !c->attr.pointer) > return true; > return false; > } > > I will wait for Paul to review. > > Thanks.. > > > > > > Cheers, > > Harald > > > >> OK for mainline. > >> > >> Regards, > >> > >> Jerry > >> --- > >> 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. > >> --- > >> > >> > >> > >> > >> > >> > >> > >> > > >
pdt_86_extra.f90
(text/x-fortran, 791 B)
! { dg-do compile }
! { dg-options "-fdump-tree-original" }
!
! Check the fix for the memory leak in pdt_77.f03. This test is a reduced
! version of the original that isolates the source of the leak. Note that
! the leak is not PDT specific.
!
module input_output_pair_m
implicit none
type tensor_t ! No longer a PDT!
real(kind(1e0)), allocatable :: values_(:)
end type
end module
use input_output_pair_m
call test
contains
subroutine test
implicit none
type(tensor_t), allocatable :: inputs(:)
integer :: i
inputs = [(tensor_t([real(i)]), i=1,2)] ! Leaked 8 bytes in 2 blocks.
end
end
! { dg-final { scan-tree-dump-times "__builtin_malloc" 7 "original" } }
! { dg-final { scan-tree-dump-times "__builtin_free" 8 "original" } }