3

I am facing the following problem and can not figure out what is happening.

I have a routine that allocates some working arrays in the beginning of my code. These working arrays are part of data structure.

The structure is defined as:

 Type bond_stat
  Real*8,Allocatable,Dimension( : , : ) ::data_space
  Real*8,Allocatable,Dimension( : , : ) ::data_time
  Real*8,Allocatable,Dimension( : )     ::bin_width         
  Real*8,Allocatable,Dimension( : )     ::time
  Integer,Allocatable,Dimension( : , : )  ::connection_table
 End Type bond_stat
 Type( bond_stat ),allocatable,dimension( : ) ::Hbonds

This is done inside a module. The next step what I am doing is to allocate this structure as

Allocate( Hbonds( 1:2 ) )

Then I call a subroutine where the arrays that are contained in the structure are allocated. The strange thing is now if I try to allocate the arrays with

 Allocate( Hbonds( i )%data_space( 1 : 3 , 1 : Nbins ) )
 Allocate( Hbonds( i )%data_time( 1 : 1 , 1 : Nbins ) )
 Allocate( Hbonds( i )%time( 1 : N ) )
 Allocate( Hbonds( i )%bin_width( 1 : 2 ) )
 Allocate( Hbonds( i )%connection_table( 1 : 2 , 1 : N ) ) )

If I do so the code states while execution the time array for example is already allocated. I 100% sure I do not allocate this array before. I am also sure the array is not used before and maybe the compiler allocates it since it is already in use. So I thought I would make a check if the array is already allocated and if it is allocated then deallocate it and reallocate it. If I do so I receive a segmentation fault in the deallocation line:

    If ( Allocated( Hbonds( i )%time ) ) then
       Deallocate( Hbonds( i )%time )
    End If
    Allocate( Hbonds( i )%time( 1 : N ) )

I am completely puzzled what is going on here. Please can anybody help. The output looks as:

Program received signal SIGSEGV: Segmentation fault - invalid memory 
reference.
Backtrace for this error:
#0  0x2b54685d3f0d in ???
#1  0x2b54685d314d in ???
#2  0x2b5468e4acaf in ???
#3  0x2b5468e9715c in ???
#4  0x4d27cf in __bond_statistics_MOD_bond_stat_init
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:35
#5  0x4c8f5f in __bond_statistics_MOD_prep_bond_stat
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:
350
#6  0x44477a in ???
#7  0x446724 in ???
#8  0x2b5468e35f44 in ???
#9  0x4016c8 in ???
#10  0xffffffffffffffff in ???
Segmentation fault (core dumped)

And when not trying to deallocate the array before it looks like:

At line 38 of file H_bond_analysis.f90
Fortran runtime error: Attempting to allocate already allocated variable 'hbonds'
Error termination. Backtrace:
#0  0x2ae8991bef0d in ???
#1  0x2ae8991bfa45 in ???
#2  0x2ae8991bfdfa in ???
#3  0x4d28e5 in __bond_statistics_MOD_bond_stat_init
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:38
#4  0x4c8f5f in __bond_statistics_MOD_prep_bond_stat
at/fs/home/jona/programs/gfortran/eval_new01/playground/H_bond_analysis.f90:350
#5  0x44477a in ???
#6  0x446724 in ???
#7  0x2ae899a20f44 in ???
#8  0x4016c8 in ???
#9  0xffffffffffffffff in ???

with line 38 being the line where time is allocated. I am using gfortran compiler with flags FLAGS = -fopenmp -g -Wall -fcheck=all -fbounds-check

zodiac
  • 291
  • 2
  • 15
  • 1
    What is `Nbins` ? – PTRK Mar 22 '18 at 14:23
  • If it says it is allocated, print out the `size()` to see what it is allocated to. – John Alexiou Mar 22 '18 at 14:23
  • Putting every piece of code ( with i = 1, nbins = 10, N = 5 ) in a single program doesn't raise an error with gfortran. The problem might be the way your subroutine is called. Could you defined the scope of every piece of code you wrote ? Like 'this one belongs to a module', 'this one is called by that', etc. – PTRK Mar 22 '18 at 14:30
  • 4
    Please do not say "it says", copy and paste the actual output into the question including line numbers and so on. The complete error message. Also, such errors can be caused by some memory corruption. I even reckon that it is very likely. Therefore you absolutely **must** prepare a [mcve] because the error can very easily be in some code you do not show. – Vladimir F Героям слава Mar 22 '18 at 14:31
  • 2
    Don't forget to enable all debugging options gfortran has `-g -Wall -fcheck=all`. Then try sanitizations instead `-g -fsanitize=address,undefined`. Then try `valgrind`. But a [mcve] is *absolutely necessary*. – Vladimir F Героям слава Mar 22 '18 at 14:35
  • @PTRK this is just some Integer that should define the size of the array. And if I print the size of the array it is either a very high positive or negative integer – zodiac Mar 22 '18 at 14:40
  • So what is line 35? Deallocate? You really need to do also the other checks and try to reduce your code to a [mcve]. I don't think anyone can help you otherwise without a magic crystal ball because what is happening in your program is too strange. Heap corruption is extremely likely. – Vladimir F Героям слава Mar 22 '18 at 15:14
  • @Vladimir Yes! Thank you for your hints. I am trying with a minimal working example right now, but this is working somehow... So probably the error has to be somewhere else – zodiac Mar 22 '18 at 15:24
  • So what I did now is to return to the last working version I had. Next I changed everything step by step. This included also the allocation of the mentioned arrays. Now I do not run into any troubles anymore although not quite sure what was going on since I don't see a difference between the new and old version with vimdiff. Maybe I made a mistake when compiling the modules or something. Because those I recompiled now also – zodiac Mar 23 '18 at 09:24

1 Answers1

0

I figured now out where the problem was. I am using a makefile to compile the code. Today I wanted to add some more arrays to the bond_stat module and after executing the makefile I ran into the same error again. What I did now is to recompile all the modules by hand without changing anything in the code itself. This solved the problem.

zodiac
  • 291
  • 2
  • 15
  • 2
    So looks like an error in the Makefile in respect to dependencies, i.e. not all file that```use``` the module are recompiled when the module is recompiled changed. – albert Apr 05 '18 at 08:52