title: Fortran 90 function return pointer
body: <p>I saw this question:</p>
  
  <p><a href="https://stackoverflow.com/questions/14062692/fortran-dynamic-objects/14062817#14062817">Fortran dynamic objects</a></p>
  
  <p>and the accepted answer made me question if I wrote the following function safely (without allowing a memory leak)</p>
  
  <pre><code>   function getValues3D(this) result(vals3D)
       implicit none
       type(allBCs),intent(in) :: this
       real(dpn),dimension(:,:,:),pointer :: vals3D
       integer,dimension(3) :: s
       if (this%TF3D) then
         s = shape(this%vals3D)
         if (associated(this%vals3D)) then
              stop "possible memory leak - p was associated"
         endif
         allocate(vals3D(s(1),s(2),s(3)))
         vals3D = this%vals3D
       else; call propertyNotAssigned('vals3D','getValues3D')
       endif
     end function
  </code></pre>
  
  <p>This warning shows up when I run my code, but shouldn't my <code>this%vals3D</code> be associated if it was previously (to this function) set? I'm currently running into memory errors, and they started showing up when I introduced a new module with this function in it.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>I think I wasn't specific enough. I would like to make the following class, and know how to implement the class, safely in terms of memory. That is:</p>
  
  <pre><code>   module vectorField_mod
     use constants_mod
     implicit none
  
     type vecField1D
       private
       real(dpn),dimension(:),pointer :: x
       logical :: TFx = .false.
     end type
  
     contains
  
     subroutine setX(this,x)
       implicit none
       type(vecField1D),intent(inout) :: this
       real(dpn),dimension(:),target :: x
       allocate(this%x(size(x)))
       this%x = x
       this%TFx = .true.
     end subroutine
  
     function getX(this) result(res)
       implicit none
       real(dpn),dimension(:),pointer :: res
       type(vecField1D),intent(in) :: this
       nullify(res)
       allocate(res(size(this%x)))
       if (this%TFx) then
         res = this%x
       endif
     end function
  
     end module
  </code></pre>
  
  <p>Where the following code tests this module</p>
  
  <pre><code>   program testVectorField
     use constants_mod
     use vectorField_mod
     implicit none
  
     integer,parameter :: Nx = 150
     real(dpn),parameter :: x_0 = 0.0
     real(dpn),parameter :: x_N = 1.0
     real(dpn),parameter :: dx = (x_N - x_0)/dble(Nx-1)
     real(dpn),dimension(Nx) :: x = (/(x_0+dble(i)*dx,i=0,Nx-1)/)
     real(dpn),dimension(Nx) :: f
     real(dpn),dimension(:),pointer :: fp
     type(vecField1D) :: f1
     integer :: i
  
     do i=1,Nx
      f(i) = sin(x(i))
     enddo
  
     do i=1,10**5
       call setX(f1,f) ! 
       f = getX(f1) ! Should I use this? 
       fp = getX(f1) ! Or this?
       fp =&gt; getX(f1) ! Or even this?
     enddo
     end program
  </code></pre>
  
  <p>Currently, I'm running on windows. When I CTR-ALT-DLT, and view performance, the "physical memory usage histery" increases with every loop iteration. This is why I assume that I have a memory leak. </p>
  
  <p>So I would like to repose my question: Is this a memory leak? (The memory increases with every one of the above cases). If so, is there a way I avoid the memory leak while still using pointers? If not, then what is happening, should I be concerned and is there a way to reduce the severity of this behavior?</p>
  
  <p>Sorry for the initial vague question. I hope this is more to the point.</p>