4 Replies Latest reply on Nov 29, 2007 10:43 PM by 807575

    iso_varying_string.f95 and Error 1183:  deallocating an unallocated allocat

    807575
      I've been trying to get a function to read a character string of arbitrary length from the command line. I have this working on other platforms/compilers but don't seem to be able to get it to work using Sun f95 when I optimize the code. I have two files, test.f95 (see below) and iso_varying_string.f95 (http://www.fortran.com/iso_varying_string.f95). If I do
      $ f95 iso_varying_string.f95 test.f95 -o test.exe -O2
      iso_varying_string.f95:
      test.f95:
      Linking:
      $ test.exe a b
       a:b
      everything works fine (same for lower levels of optimization). But if I do:
      $ f95 iso_varying_string.f95 test.f95 -o test.exe -O3
      iso_varying_string.f95:
      test.f95:
      Linking:
      $ test.exe a b
      
       ******  FORTRAN RUN-TIME SYSTEM  ******
       Error 1183:  deallocating an unallocated allocatable array
       Location:  the DEALLOCATE statement at line 11 of "test.f95"
      Abort
      It crashes (same for higher levels of optimization). I haven't been able to track down the cause of this (although it looks like some type of memory corruption). Any ideas?

      For reference:
      $ f95 -V
      f95: Sun Fortran 95 8.3 SunOS_i386 Build40_1 2007/02/08
      -----
      test,f95
      program test
        use iso_varying_string
        type (varying_string) :: odir,inputfile
      
        call Get_Argument_Varying_String(1,odir)
        call Get_Argument_Varying_String(2,inputfile)
      
        write (0,*) char(odir),':',char(inputfile)
      
      contains
        subroutine Get_Argument_Varying_String(i,a)
          ! Reads a command line argument into a varying string variable.
          implicit none
          integer,               intent(in)  :: i
          type (varying_string), intent(out) :: a
          integer                            :: tmp_len, stat
          integer, parameter                 :: ARG_MAXLEN=500
      
          stat=ARG_MAXLEN
          tmp_len=0
          ! Loop until we've used a temporary string large enough to get the entire argument.
          do while (stat.ge.tmp_len)
             tmp_len=tmp_len+ARG_MAXLEN
             call Get_Temporary_String(i,a,tmp_len,stat)
          end do
          return
        end subroutine Get_Argument_Varying_String
      
        subroutine Get_Temporary_String(i,vchar,tmp_len,stat)
          ! Makes use of a temporary array as effectively a dynamically sized character array for temporary storage for reading in a
          ! command line argument of arbitrary size.
          implicit none
          integer,               intent(in)  :: i,tmp_len
          integer,               intent(out) :: stat
          type (varying_string), intent(out) :: vchar
          character(len=tmp_len)             :: tmp_string
      
          call getarg(i,tmp_string)      ! Get the argument.
          stat=len_trim(tmp_string)
          vchar=trim(tmp_string)         ! Transfer to the variable length string.
          return
        end subroutine Get_Temporary_String
      
      end program test