https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61830

            Bug ID: 61830
           Summary: Memory leak with assignment to array of derived types
                    with allocatable components
           Product: gcc
           Version: 4.8.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sfilippone at uniroma2 dot it

Created attachment 33132
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33132&action=edit
test case

Hi,
The attached code shows the problem. 
When I started investigating I was using 4.10 but I ran into PR61819 while
reducing the test case. 

With 4.8.3 I get the following:
--------------------------------------------
[sfilippo@jacobi runs]$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/local/gnu/4.8.3/libexec/gcc/x86_64-unknown-linux-gnu/4.8.3/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.8.3/configure --prefix=/usr/local/gnu/4.8.3
--enable-languages=c,c++,fortran --with-gmp=/home/travel/GNUBUILD/gmp
--with-mpfr=/home/travel/GNUBUILD/mpfr --with-mpc=/home/travel/GNUBUILD/mpc
Thread model: posix
gcc version 4.8.3 (GCC) 
[sfilippo@jacobi runs]$ valgrind --leak-check=full ./foo-test-leak 
==20638== Memcheck, a memory error detector
==20638== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==20638== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==20638== Command: ./foo-test-leak
==20638== 
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
==20638== 
==20638== HEAP SUMMARY:
==20638==     in use at exit: 6,164 bytes in 23 blocks
==20638==   total heap usage: 89 allocs, 66 frees, 35,518 bytes allocated
==20638== 
==20638== 560 (48 direct, 512 indirect) bytes in 1 blocks are definitely lost
in loss record 3 of 5
==20638==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==20638==    by 0x400B70: __foo_base_mod_MOD_foo_geall (foo-test-leak.f90:96)
==20638==    by 0x401F8B: __foo_scalar_field_mod_MOD_new_scalar_field
(foo-test-leak.f90:141)
==20638==    by 0x40271F: __foo_vector_field_mod_MOD_new_vector_field
(foo-test-leak.f90:163)
==20638==    by 0x402AC8: MAIN__ (foo-test-leak.f90:188)
==20638==    by 0x4031D3: main (foo-test-leak.f90:179)
==20638== 
==20638== 5,600 (480 direct, 5,120 indirect) bytes in 10 blocks are definitely
lost in loss record 5 of 5
==20638==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
==20638==    by 0x400B70: __foo_base_mod_MOD_foo_geall (foo-test-leak.f90:96)
==20638==    by 0x401F8B: __foo_scalar_field_mod_MOD_new_scalar_field
(foo-test-leak.f90:141)
==20638==    by 0x40271F: __foo_vector_field_mod_MOD_new_vector_field
(foo-test-leak.f90:163)
==20638==    by 0x402C9E: MAIN__ (foo-test-leak.f90:192)
==20638==    by 0x4031D3: main (foo-test-leak.f90:179)
==20638== 
==20638== LEAK SUMMARY:
==20638==    definitely lost: 528 bytes in 11 blocks
==20638==    indirectly lost: 5,632 bytes in 11 blocks
==20638==      possibly lost: 0 bytes in 0 blocks
==20638==    still reachable: 4 bytes in 1 blocks
==20638==         suppressed: 0 bytes in 0 blocks
==20638== Reachable blocks (those to which a pointer was found) are not shown.
==20638== To see them, rerun with: --leak-check=full --show-reachable=yes
==20638== 
==20638== For counts of detected and suppressed errors, rerun with: -v
==20638== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6)
-------------------------------------------------------------------------------

If I change line 163 
    this%u = [new_scalar_field()]

into
    do i=1, size(this%u)
      associate(sf=>this%u(i))
        sf = new_scalar_field()
      end associate
    end do

then I get no leak

[sfilippo@jacobi runs]$ valgrind --leak-check=full ./foo-test-leak-fixed
==20852== Memcheck, a memory error detector
==20852== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==20852== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==20852== Command: ./foo-test-leak-fixed
==20852== 
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
 Scalar deallocation
 Deallocate class() component
==20852== 
==20852== HEAP SUMMARY:
==20852==     in use at exit: 4 bytes in 1 blocks
==20852==   total heap usage: 67 allocs, 66 frees, 29,364 bytes allocated
==20852== 
==20852== LEAK SUMMARY:
==20852==    definitely lost: 0 bytes in 0 blocks
==20852==    indirectly lost: 0 bytes in 0 blocks
==20852==      possibly lost: 0 bytes in 0 blocks
==20852==    still reachable: 4 bytes in 1 blocks
==20852==         suppressed: 0 bytes in 0 blocks
==20852== Reachable blocks (those to which a pointer was found) are not shown.
==20852== To see them, rerun with: --leak-check=full --show-reachable=yes
==20852== 
==20852== For counts of detected and suppressed errors, rerun with: -v
==20852== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

Reply via email to