deallocation and memory allocation problems in FORTRAN

Go To


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.


     dummy allocation

   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


       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: =========
   .    /adatptmultistage1new.out[0x43247c]
   .    /adatptmultistage1new.out[0x404368] 
  .    /adatptmultistage1new.out[0x4030e9]
   ======= Memory map: ========
00400000-004d4000 r-xp 00000000 08:03    9642201 
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]
2012-04-04 20:59
by Jourdan Gold
Can you fix your source code formatting. Indentation helps too - Chris 2012-04-04 21:25 - eriktous 2012-04-04 23:49
You don't check the status code returned by any of your allocate or deallocate statements. Doing so might provide some useful diagnostic information - High Performance Mark 2012-04-05 05:03
Even after editing, this is still a massive amount of code to go through. Please revise your question to clearly state what you've done to debug, and try to trim the code samples down to only parts that are relevant. If you can do that, please flag your question for moderator review - Tim Post 2012-04-06 07:47


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
2012-04-04 22:17
by M. S. B.
Hi I will try your suggestions. my apologies on the amount of code. I am using a code and then modifying it to add more complexity. I am using ifort. what do you mean by array bounds checkin - Jourdan Gold 2012-04-05 16:00
ok, so I have done that and while it helped me by informing me of variables that were no longer used within the program. I can't see any thing obvious from the messages that it provided. I am posting the message from applying these options so that you can see it and hopefully interpret it better than I can. I really appreciate your help. thank yo - Jourdan Gold 2012-04-05 21:19
It looks that you now have a problem with linking. Are there any earlier error or warning messages - M. S. B. 2012-04-06 00:05
not sure what you mean by linking .. sorry. in full checking mode, I get warning that the system is not fully unhappy with the fact that I have a few very long lines namely the filenames in the open command. problem always occurs right after the third iteration of my f loop. so it outputs several files about 512 different results files before it goes wonky. I should add that the status command does not stop the program which suggests that it has ok status but the program does not print output from after the deallocate. i.e. the program gets stuck at this location. thank - Jourdan Gold 2012-04-06 02:57
Re linking: I was referring to all the error messages about "multiple definition of X". Are you opening too many files for your OS? Do you close files after you are done with them? Have you run the program after compiling with "-check all" or "-check bounds" - M. S. B. 2012-04-06 03:47