Following code causes severe memory leak with gfortran 4.6.0 (also with trunk revision 174463) program ala implicit none type :: array_of_foo real, dimension(:), allocatable :: p end type array_of_foo type(array_of_foo), dimension(:), allocatable :: tab integer, parameter :: lb = 0 integer, parameter :: ub = 2 integer, parameter :: nmax = 10 integer :: i real :: ran do while(.true.) allocate(tab(lb:ub)) allocate(tab(lb)%p(0)) do i = lb+1, ub allocate(tab(i)%p(nmax)) tab(i)%p(:) = [(i,i=1,nmax)] enddo tab(lb)%p = [tab(lb)%p, tab(ub)%p] !!! mem leak if first array has size 0 ! tab(lb)%p = [tab(ub)%p, tab(lb)%p] !!! no mem leak ! clean up do i = lb, ub deallocate(tab(i)%p) enddo deallocate(tab) enddo end program ala lhs realloc of size(0) = [size(0), size(n)] leaks, whereas size(0) = [size(n), size(0)] works fine. Following code does not exhibit this behaviour with Intel compiler (11.1.072)
The memory the program needs increases continuously as "top" shows; it takes about 5min on my computer before the OS kills it (out of memory). If one does not use an endless loop but a finite loop, it shows no leakage with neither valgrind nor totalview. Also the result seems to be fine - except that the memory consumption simply grows ... Slightly reduced test case: program ala implicit none type :: array_of_foo real, dimension(:), allocatable :: p end type array_of_foo type(array_of_foo), dimension(:), allocatable :: tab integer :: i do allocate(tab(0:1)) allocate(tab(0)%p(0)) tab(1)%p = [(i,i=1,10)] ! Realloc-on assignment (fine!) tab(0)%p = [tab(0)%p, tab(1)%p] ! << realloc-on-assignment. Culprit? deallocate(tab) end do end program ala
(In reply to comment #1) > The memory the program needs increases continuously as "top" shows; it takes > about 5min on my computer before the OS kills it (out of memory). > > If one does not use an endless loop but a finite loop, it shows no leakage with > neither valgrind nor totalview. Also the result seems to be fine - except that > the memory consumption simply grows ... > > Slightly reduced test case: It's not caused by reallocation, rather by array constructor? Test case without lhs realloc: program ala implicit none type :: array_of_foo real, dimension(:), allocatable :: p end type array_of_foo type(array_of_foo), dimension(:), allocatable :: tab integer :: i, nn do allocate(tab(1:2)) allocate(tab(1)%p(0)) allocate(tab(2)%p(1)) tab(2)%p(1) = 1.0 nn = size( [tab(1)%p, tab(2)%p] ) deallocate(tab(2)%p) deallocate(tab(1)%p) deallocate(tab) end do end program ala
phsical memory main point
This doesn't reproduce for me with trunk (4.8) nor with 4.7. It does fail with 4.6. So I would say has been fixed as part of some other bug.
Fixed by revision 188062 and branches: 4.5, 4.6 and 4.7. *** This bug has been marked as a duplicate of bug 53521 ***