Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Bug in Final elemental procedure ?

reubendb
Beginner
1,040 Views
Hello,
There seems to be a bug in final procedure if the procedure is elemental. The following example (module and program) illustrate the bug:
[fortran]module RaggedArrayModule implicit none private type, public :: RaggedArrayInteger integer, dimension(:), allocatable :: Value contains procedure, public, pass :: Initialize final :: Finalize end type RaggedArrayInteger contains subroutine Initialize(A, nValues) class(RaggedArrayInteger), intent(inout) :: A integer, intent(in) :: nValues allocate (A%Value(nValues)) end subroutine Initialize elemental subroutine Finalize(A) type(RaggedArrayInteger), intent(inout) :: A if (allocated(A%Value)) deallocate(A%Value) end subroutine Finalize end module RaggedArrayModule program RaggedArray_Test use RaggedArrayModule type(RaggedArrayInteger), dimension(:), allocatable :: MyArray !-- This works as expected allocate (MyArray(1)) call MyArray(1)%Initialize(10) deallocate(MyArray) !-- Allocating array with size zero is legal, but produce ! segfault in deallocation when final procedure is elemental allocate(MyArray(0)) deallocate(MyArray) end program RaggedArray_Test [/fortran]
In other words, I think the issue is because when the size of MyArray is allocated to zero, the deallocation triggers the elemental final procedure "Finalize()" and try to access its first element when the actual size is zero. I believe this is a behavior is a bug.
0 Kudos
3 Replies
reubendb
Beginner
1,040 Views
I forgot to mention the compiler version I'm using:

$ ifort -V
Intel Fortran Intel 64 Compiler XE for applications running on Intel 64, Version 12.1 Build 20111128
Copyright (C) 1985-2011 Intel Corporation. All rights reserved

Thanks.
RDB
0 Kudos
velvia
Beginner
1,040 Views

It is useless to deallocate an allocatable component in a final procedure. Remember that allocatable components are just "smart pointers" and are made to deallocate the memory they refer to when they go out of scope.

0 Kudos
Steven_L_Intel1
Employee
1,040 Views
Interesting. We'll take a look at this.
0 Kudos
Reply