[flang-commits] [flang] [flang] Refine handling of NULL() actual to non-optional allocatable … (PR #116126)

Daniel Chen via flang-commits flang-commits at lists.llvm.org
Fri Nov 15 07:53:03 PST 2024


DanielCChen wrote:




> Should lowering now generate conditional deallocation after the call in case the dummy argument associated to NULL is allocated inside the function?
> 
> Currently, lowering does not do that (relying on the INTENT(IN) aspect). But with this patch, the following code will now compile (with a warning) and leak memory:
> 
> ```
> module m
> contains
> subroutine foo(x)
>   real, allocatable :: x(:)
>   allocate(x(10))
> end subroutine
> end module
>   use m, only : foo
>   do i=1,10
>   call foo(null())
>   end do
> end
> ```
> 
> valgrind:
> 
> ```
> ==2914349== HEAP SUMMARY:
> ==2914349==     in use at exit: 400 bytes in 10 blocks
> ==2914349==   total heap usage: 10 allocs, 0 frees, 400 bytes allocated
> ==2914349== 
> ==2914349== LEAK SUMMARY:
> ==2914349==    definitely lost: 400 bytes in 10 blocks
> ```

I would think this is user's responsibility.
I think they should also check if `x` is allocated or not before the `allocate` statement and deallocate before exit if the allocate is inside of `foo`.

https://github.com/llvm/llvm-project/pull/116126


More information about the flang-commits mailing list