I'm having an issue with my Fortran 90 code involving deallocating an array that is declared in a module and then allocated & initialized within a subroutine. Within my program, I declare a bunch of arrays in modules like real*8, dimension(:), allocatable :: test
. Then, in an initialization subroutine, I use
the module, allocate the variable with
allocate(test(8))
, and initialize it with test = 0.d0
.
After this, I can print*, test
and get the appropriate output: 0.E+0 0.E+0 0.E+0 0.E+0 0.E+0 0.E+0 0.E+0 0.E+0
. In addition, a call to allocated(test)
returns .TRUE.
. Nevertheless, something goes wrong. Directly after the call to allocate()
, a call to sizeof(test)
returns 0
and a call to deallocate(test)
throws the following error:
lib-4422 : UNRECOVERABLE library error
A DEALLOCATE statement argument points to a portion of the
original allocation.
Original size in bytes for the argument is 512
Current size in bytes for the argument is 0
Segmentation fault
This all occurs within a larger code, throughout which I've used these arrays with no errors. I only noticed the problem when I tried to deallocate the memory at the end of the program while hunting for a memory leak. I've tried to make a simple program that only does what has been described above (i.e., declare in module, allocate & initialize in subroutine, then print the array and deallocate it within the same subroutine). This simple code, however, works properly and runs without error. Thus, I'm very confused at what could be causing this to misbehave within the context of the larger code. Furthermore, if within my larger code I move the declaration line from the module to the subroutine, everything runs properly.
Any advice would be appreciated! Thanks in advance,
~BCL