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

Memory leak allocating a pointer to a type

Jacob_Williams
New Contributor III
887 Views

This is just a toy example where I'm allocating a pointer to a type, and then immediately nullifying it.  When calling this in a loop the memory used steadily increases until the program eventually crashes with "forrtl: severe (41): insufficient virtual memory".  Using the latest version of the compiler (14.0.1.139, 32bit) on Windows 7.  Am I doing something crazy here?  I tend to avoid pointers, but I have a situation where I need to use them (linked lists).  My application is leaking memory, and I've reduced the problem down to this.  My understanding is that this code is OK, but maybe I'm totally confused.  Any help is appreciated.

[fortran]

   module test_module
   implicit none
      type,public :: blah
         character(len=10000)      :: a = ''
         integer,dimension(100,100)   :: b = 0
      end type blah
   contains

      subroutine memory_leak()
      implicit none

      type(blah),pointer :: p

      write(*,*) 'blah'

      allocate(p)
      !
      ! do stuff with p...
      !
      nullify(p)

      end subroutine memory_leak

   end module test_module
   program test
      use test_module
      integer :: i
      i=0
      do
         i=i+1
         write(*,*) i
         call memory_leak()
      end do
   end program test

[/fortran]

0 Kudos
10 Replies
Steven_L_Intel1
Employee
887 Views

Do you perhaps think that nullifying a pointer deallocates the memory? It doesn't. Use DEALLOCATE for that. Typically you nullify a pointer to ensure that it is in a known state so you can test for it later. The only time you'd want to nullify a pointer after allocation is if you had already pointed some other pointer at the storage.

0 Kudos
Jacob_Williams
New Contributor III
887 Views

Wow, I was assuming that!  If I replace nullify with allocate in this example, then the leak goes away.  However, if I modify the toy code a bit to be more like the actual code, the leak returns.  See attached file.  The type now contains a polymorphic class, and I'm using a subroutine to do the pointer deallocation.  This is leaking memory.

Interestingly, with this example, if I uncomment the other variables in the type (a and b), the destroy routine gives an access violation.

[Note: this code is triggering the forum spam filter when I paste it in the field.  Let's see if it will upload as a file.]

 

0 Kudos
Steven_L_Intel1
Employee
887 Views

Ok, I can reproduce this. Very strange. I will look into it.

0 Kudos
Steven_L_Intel1
Employee
887 Views

Escalated as issue DPD200252621. I will let you know what we find. At first I thought the empty type was a trigger, as we've had issues with those in the past, but not in this case.

0 Kudos
Jacob_Williams
New Contributor III
887 Views

Thanks!  Yeah, my actual code does have data in the type.  It's basically a linked list of a polymorphic type.  Keep me posted.  As an experiment, I think I will remove the polymorphic class and do it the old fashioned way...just to see if I can get that working without leaking memory.

0 Kudos
Izaak_Beekman
New Contributor II
887 Views

What is the status of DPD200252621?

0 Kudos
Steven_L_Intel1
Employee
887 Views

Still being investigated.

0 Kudos
Izaak_Beekman
New Contributor II
887 Views

Thanks Steve

0 Kudos
Steven_L_Intel1
Employee
887 Views

It looks as if this is now fixed for the 15.0 version to be released later this year. It isn't fixed in the initial beta.

0 Kudos
Jacob_Williams
New Contributor III
887 Views

Just verified that it is indeed fixed in 2015. Thanks!!

0 Kudos
Reply