Memory leak allocating a pointer to a type

Memory leak allocating a pointer to a type

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 (, 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.

   module test_module

	   implicit none

	      type,public :: blah

	         character(len=10000)      :: a = ''

	         integer,dimension(100,100)   :: b = 0

	      end type blah

	      subroutine memory_leak()

	      implicit none
	      type(blah),pointer :: p
	      write(*,*) 'blah'


	      ! do stuff with p...


	      end subroutine memory_leak
	   end module test_module

	   program test

	      use test_module

	      integer :: i




	         write(*,*) i

	         call memory_leak()

	      end do

	   end program test

11 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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.

Retired 12/31/2016

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.]



Downloadapplication/octet-stream test_0.f901.04 KB

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

Retired 12/31/2016

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.

Retired 12/31/2016

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.

What is the status of DPD200252621?


Still being investigated.

Retired 12/31/2016

Thanks Steve


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.

Retired 12/31/2016

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

Leave a Comment

Please sign in to add a comment. Not a member? Join today