aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gfortran.dg/finalize_57.f90
blob: b6257357c752c0f78c312aa33dab57c75c14c8c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
! { dg-do compile }
! { dg-additional-options "-fdump-tree-original" }
!
! PR fortran/90068
!
! Contributed by Brad Richardson  <everythingfunctional@protonmail.com>
! 

program array_memory_leak
    implicit none

    type, abstract :: base
    end type base

    type, extends(base) :: extended
    end type extended

    type :: container
        class(base), allocatable :: thing
    end type

    type, extends(base) :: collection
        type(container), allocatable :: stuff(:)
    end type collection

    call run()
    call bad()
contains
    subroutine run()
        type(collection) :: my_thing
        type(container) :: a_container

        a_container = newContainer(newExtended()) ! This is fine
        my_thing = newCollection([a_container])
    end subroutine run

    subroutine bad()
        type(collection) :: my_thing

        my_thing = newCollection([newContainer(newExtended())]) ! This is a memory leak
    end subroutine bad

    function newExtended()
        type(extended) :: newExtended
    end function newExtended

    function newContainer(thing)
        class(base), intent(in) :: thing
        type(container) :: newContainer

        allocate(newContainer%thing, source = thing)
    end function newContainer

    function newCollection(things)
        type(container), intent(in) :: things(:)
        type(collection) :: newCollection

        newCollection%stuff = things
    end function newCollection
end program array_memory_leak

! { dg-final { scan-tree-dump-times "__builtin_free" 15 "original" } }