Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28457 Discussions

allcoatable arrays can't be allocated and allocated arrays can't be deallocated

Wang__Dezhi
Beginner
543 Views

Hi,

Here is the code:

                   ......

                    deallocate(chebft_y)                      

                    N2wave=int((xrhomax2-xrhomin)/(2*xwavebjmin))      
                    if (N2wave .lt. 1) then
                        !c       if (.true.) then
                        !ccccccccccccccccc
                        !ccccc      
                        write(*,*)'N2wave is less than 1.'  
                        mxy=int(mxyunit1*(xrhomax2-xrhomin)/(2*xwavebjmin))
                        if (mxy .lt. NPmin) then
                            mxy = NPmin
                        endif
                        write(*,*)'mxy=', mxy
                        allocate(rkho(0:mxy-1),gllY(mxy),gllX(mxy),gllW(mxy))      

                        ......

The array chebft_y is allocated ( I have checked its elements), but when deallocating it, it says 'BSEM_90.exe has triggered a breakpoint' and a No Source Available window comes out. But in the debugging window, no error hints show. 

I tried putting this part into a subroutine which is also used in other place. When first call this subroutine, no problems shows. However, when call this subroutine the second time, a similar problem shows. But the strange thing is sometimes the chebft_y can be allocated, sometimes it can't be. When it can be allocated, the last allocate operation for (rkho, gllY...) can not run and also shows ' exe has triggered a breakpoint' and a runtime error window saying 'R6025 - pure virtual function call', still no other error hints in the debugging window. 

Where may be the problem? Thanks~ If need further information is needed, please tell me!

 

 

0 Kudos
9 Replies
jimdempseyatthecove
Honored Contributor III
543 Views

is chebft_y an array declared as ALLOCATABLE...
.OR. is it a POINTER to an array?

Note, a pointer, if not initialized (e.g. => NULL(), or NULLIFY(ptr)) will more likely than not contain junk data, and therefore appear associated, and may even appear allocated. The deallocate-ing of such an object is subject to program crash or corruption.

Can you show more of your program? This can have unrelated lines remove, replace with ...code...

Show how you declare the array, allocate the array and use the array (IOW is it passed into the subroutine as a dummy argument, and if so, show how it is declared in the subroutine).

Jim Dempsey

0 Kudos
Wang__Dezhi
Beginner
543 Views

Thanks so much for the reply.

chebft_y is declared in another module. The followings are the declaring module, the subroutine with problem, and main program calling the subroutine.

The declaring module

module interpolation_1
 ...

      real,allocatable:: chebft_y(:)
    contains

...

        if (interpolationID .eq. 4)    then
            allocate(chebft_y(mxy_subroutine))

...

end module

The module containing the module

module field_inc_sct

...

   subroutine interpo_rkho(intid,iNff,interpolationid1,mxyunit1,NPmin,xrhomin1,xrhomax1,xwavebgmin,xwavebjmin,mxy,rkho)
                                              ! the iNff only works when intid=1   
      implicit none   
       integer,intent(in):: intid,iNff,interpolationid1,mxyunit1,NPmin
       real,intent(in):: xrhomin1,xrhomax1,xwavebgmin,xwavebjmin
       real,allocatable,intent(out):: rkho(:)       
       ...
        real,allocatable:: gllX(:),gllY(:),gllW(:)

       ...

       else if (interpolationid1 .eq. 4) then
           if(intid .eq.1) then
                  if(iNff.gt.1) deallocate(chebft_y)
           endif           
           if(intid.eq.2) then
               deallocate(chebft_y)
           endif           
           N2wave=int((xrhomax-xrhomin)/(2*xwavebjmin))      
           if (N2wave .lt. 1) then
           ...    
           else
                  mxy = mxyunit1
                  allocate(rkho(0:mxy*N2wave+mxy-1),gllX(mxy),gllY(mxy),gllW(mxy))    
                  CALL GLL(gllX,gllW,mxy-1)      
               do iN2wave = 1,N2wave
                   xrhominID = xrhomin + (iN2wave-1)*2*xwavebjmin      
                   xrhomaxID = xrhomin + iN2wave*2*xwavebjmin            
                   do ixy = 1,mxy
                       gllY(ixy) = 0.50*(xrhomaxID - xrhominID)*gllX(ixy) +      &
                                           0.50*(xrhominID + xrhomaxID)         
                       rkho(ixy-1+mxy*(iN2wave-1)) = gllY(ixy)                    
                   enddo                    
               enddo      
               mxy=int(mxyunit1*(xrhomax-N2wave*2*xwavebjmin-xrhomin)/(2*xwavebjmin))                            
               ...
              CALL GLL(gllX,gllW,mxy-1)    
               xrhominID = xrhomin + N2wave*2*xwavebjmin      
               xrhomaxID = xrhomax            
               do ixy = 1,mxy
                   gllY(ixy) = 0.50*(xrhomaxID - xrhominID)*gllX(ixy) +      &
                                       0.50*(xrhominID + xrhomaxID)         
                   rkho(mxyunit1*N2wave+ixy-1) = gllY(ixy)
               enddo            
                mxy = mxyunit1*N2wave + mxy    
                deallocate(gllX,gllY,gllW)
           endif  

         endif
    end subroutine

end module

Then main program calling the subroutine, here rkho is declared as allocatable, but without allocation.

          ...

          intid=1 ! the iNff only works when intid=1          
          call   interpo_rkho(intid,iNff,interpolationid1,mxyunit1,NPmin,xrhomin,xrhomax1,xwavebgmin,xwavebjmin,mxy,rkho)

          ! this is the first time calling this subroutine, no problems are showed.Before this calling, chebft_y  has not been allocated.

          ...

          deallocate(rkho)

           ...

                   intid=2                  
                  iNff1=2 ! this iNff1 is an arbitrary number, which will make no sense to this subroutine because intid=2
                  call interpo_rkho(intid,iNff1,interpolationid1,mxyunit1,NPmin,xrhomin,xrhomax2,xwavebgmin,xwavebjmin,mxy,rkho)

                   ! this is the second time calling this subroutine, the problems mentioned before are showed. The chebft_y is allocated before this calling.

Are all these enough? Tell me if more information is needed. Thanks so much.

 

 

 

 

 

 

 

0 Kudos
Andrew_Smith
Valued Contributor I
543 Views

I have seen similar issues and the cause was trampling on the memory used to descibe the allocation status. This can happen if any part of your program uses arrays out of bounds, most likely when you pass wrong size arrays to libraries.

0 Kudos
Wang__Dezhi
Beginner
543 Views

Andrew, thanks so much for the help. But if the array has run out of bounds somewhere before, will the compiler not show errors? Can it continue running?

0 Kudos
Wang__Dezhi
Beginner
543 Views

I also find in the stack frame, it shows that

...

kernel32.dll 759f38f4()

[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]

ntdll.dll!77b5de3()

ntdll.dll!77b5dae()

What does this mean?

0 Kudos
jimdempseyatthecove
Honored Contributor III
543 Views

Is subroutine interpo_rkho located within a module?

Meaning its interface is shown to the callers? (a requirement for procedure dummies that are allocatable or use assumed shape "(:)" declarations).

I suggest you insert diagnostic code (often called asserts) to assert when you assume the array is allocated, that it is allocated, and when you assume the array is deallocated that it is deallocated. Do not simply coerce it into the state as this will hide an error that you have elsewhere in your program.

The compiler will not tell you about array access out of bounds, but you can compile with runtime checks enabled.

In situations like yours, it is recommended that you make a Debug build with all the runtime checks enabled (including interface checking). If this runs, then you are reasonably assured (not absolutely assured), that most of the common mistakes are caught. Note, this will not tell you why or where the allocation status of this array was made (or not made) correctly.

Also, if this array has different allocations depending on interpolationID, then it may be helpful to place additional asserts that, as an example, when interpolationID==4, and you expect the array to be allocated, that you also assert the size of the allocated array is what you expect. Catching an error like this early on will assist you in finding the point of error in your code.

Jim Dempsey

0 Kudos
Wang__Dezhi
Beginner
543 Views

Jim, thanks so much for the answer. 

The subroutine interpo_rkho is located in the module field_inc_sct. 

As for the diagnostic code, I don't know what this is and also the runtime checks. Could you please show me some links discussing how to do these? 

0 Kudos
jimdempseyatthecove
Honored Contributor III
543 Views

If you are using Visual Studio:

Open your Solution

Select Debug build

Open/Use the Solution Explorer and locate the Fortran Project containing the code in question.

Right-Click on the Fortran Project in question

Select Properties

Under: Configuration Properties > Fortran > Run-time

Enable all runtime checks

Then under:  Configuration Properties > Fortran > Diagnostics

enable Check Routine Interfaces

If you are using command line, then locate the equivalent options. If you need a list of options. Launch the appropriate command prompt with Intel configuration, then enter

CD \SomeDirectoryYouUse
ifort /? > ifortCommandLineOptons.txt
notepad ifortCommandLineOptions.txt

Jim Dempsey

0 Kudos
Wang__Dezhi
Beginner
543 Views

Jim, thanks so much for the help~

0 Kudos
Reply