deallocation and memory allocation problems in FORTRAN

Jourdan Gold picture Jourdan Gold · Apr 4, 2012 · Viewed 14.3k times · Source

I am having problems with the deallocate and allocate aspects of part of my FORTRAN code. in particular, i think that the issue has to do with memory allocation from a search on my error message on the web. The error message talks about invalid pointers, however, I am not using any pointers in my program

After completing iteration # 2 of my f loop (see below), the program crashes or rather most of the time it crashes and sometimes it just freezes up. I am confident that this is the point where the bug is. as the program runs up to this point.

I have subroutines not shown but since they work for other simulation combinations, I am reasonably confident that they are not the problem. I am using deallocate and allocate in other places within the program (successfully) so I am surprised that it is not working here.

I am only showing part of the program for ease of reading. in particular, I have removed my calls to the subroutines that I wrote. I hope that i have provided sufficient info for you programmers to help me figure out the problem. if not please specify what other info you want and I will be happy to comply. I have compiled the program using various compiler options and have fixed some bugs and removed any warnings. At this point, however, the compiler options do not give me any more info.

allocate(poffvect(1:6))
allocate(phi1out(1:1))
allocate(phi2out(1:1))
allocate(phi1outs1(1:1))
allocate(phi2outs1(1:1))

     dummy allocation
   allocate(phi1outind(1:1))
   allocate(phi2outind(1:1))
   allocate(phi1outinds1(1:1))
   allocate(phi2outinds1(1:1))

   do e = 1, 6
     print *,"e", e
     do f = 1, 3

       print *,"f", f, iteratst1(f), trim(filenumcharimp)

       deallocate(phi1outinds1, STAT = AllocateStatus)
     if (AllocateStatus /= 0) stop "Error during  deallocation of phi1outinds1"
     print *, "Allocatestatus of phi1outinds1 is",  AllocateStatus
     deallocate(phi2outinds1, STAT = AllocateStatus)
    print *, "DeAllocatestatus of phi1outinds2 is", AllocateStatus

     if (AllocateStatus /= 0) stop "Error during deallocation of phi2outinds1"
    print *, "we deallocate f loop ok", iteratst1(f)

      allocate(phi1outinds1(1:iteratst1(f)), STAT = AllocateStatus)
     if (AllocateStatus /= 0) stop "Error during allocation of phi1outinds1"
     allocate(phi2outinds1(1:iteratst1(f)), STAT = AllocateStatus)
    if (AllocateStatus /= 0) stop "Error during deallocation of phi1outinds1"

 end do
 end do

compiler options

ifort -free -check  -traceback -o adatptmultistage1new.out adatptmultistage1new.f90

output

       e           1
       f           1        5000 43
     DeAllocatestatus of phi1outinds1 is           0
    DeAllocatestatus of phi1outinds2 is           0
    we deallocate f loop ok        5000
   f loop done           1
  f           2       10000 43
 Allocatestatus of phi1outinds1 is           0
 DeAllocatestatus of phi1outinds2 is           0
 we deallocate f loop ok       10000
   f loop done           2
   f           3       15000 43
  Allocatestatus of phi1outinds1 is           0

error message

*** glibc detected *** ./adatptmultistage1new.out: munmap_chunk(): invalid pointer:   0x0000000000d3ddd0 ***
    ======= Backtrace: =========
    /lib/libc.so.6(+0x77806)[0x7f5863b7b806]
   .    /adatptmultistage1new.out[0x43247c]
   .    /adatptmultistage1new.out[0x404368] 
   ./adatptmultistage1new.out[0x4031ec]
   /lib/libc.so.6(__libc_start_main+0xfd)[0x7f5863b22c4d]
  .    /adatptmultistage1new.out[0x4030e9]
   ======= Memory map: ========
00400000-004d4000 r-xp 00000000 08:03    9642201 
/home/jgold/smwcv/error_infect/test/surfaces/multistage/adaptonly/adatptmultistage1new.out
006d4000-006dc000 rw-p 000d4000 08:03 9642201

[rest of error message not shown for brevity]

  7fffb004d000-7fffb00bc000 rw-p 00000000 00:00 0                          [stack]
  7fffb01d7000-7fffb01d8000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  Aborted

Answer

M. S. B. picture M. S. B. · Apr 5, 2012

That is a lot of code for us to try to figure out. Have you compiled it with as many compiler debugging options as possible? Especially, are you using array bounds checking? What compiler are you using? I don't see a "use" statement ... it would be better to put your subroutines into a module and "use" that module so that the compiler can check argument consistency between the actual and dummy arguments.

EDIT: "double free or corruption" suggests that memory has been corrupted. Since you don't appear to have any pointers there are three likely ways to corrupt memory:

  1. Use an allocatable variable that hasn't been allocated. If the allocate statement fails the program would probably throw an error at that point. You might be using a variable that you forget to allocate.
  2. Having a disagreement between the arguments in the call to a procedure and what the procedure expects, i.e., between actual and dummy arguments. Using a module will allow the compiler to do better checking for this.
  3. Writing outside the size of an array by using an illegal subscript value -- this will overwrite "random" memory, such as the internal structures describing the next array. Turning on run-time subscript or array-bound checking will test for this. With ifort use:-check bounds or -check all. For very through checking try: -O2 -stand f03 -check all -traceback -warn all -fstack-protector -assume protect_parens -implicitnone