1. Satish Karra
  2. karra-pflotran-dev

Commits

Glenn Hammond  committed 42a4335

Fixed memory leaks

  • Participants
  • Parent commits 0706f5e
  • Branches default

Comments (0)

Files changed (9)

File src/pflotran/dataset_base.F90

View file
  • Ignore whitespace
   call DeallocateArray(this%rbuffer)
   call DeallocateArray(this%dims)
   
+  call TimeStorageDestroy(this%time_storage)
+  
 end subroutine DatasetBaseStrip
 
 ! ************************************************************************** !

File src/pflotran/discretization.F90

View file
  • Ignore whitespace
   end select
   grid%discretization_itype=discretization%itype
   discretization%grid => grid
+  nullify(grid)
 
 end subroutine DiscretizationReadRequiredCards
 
   if (discretization%tvd_ghost_scatter /= 0) &
     call VecScatterDestroy(discretization%tvd_ghost_scatter)
   
+  ! solely nullify grid since destroyed in patch
+  call GridDestroy(discretization%grid)
+  
+  deallocate(discretization)
+  nullify(discretization)
+  
 end subroutine DiscretizationDestroy
  
 end module Discretization_module

File src/pflotran/hdf5.F90

View file
  • Ignore whitespace
   use Patch_module
   use HDF5_Aux_module
   use Unstructured_Cell_module
+  use Utility_module, only : DeallocateArray
   
   implicit none
 
   PetscInt          :: ndims
   PetscInt          :: remainder
   PetscInt          :: istart, iend, ii, jj
-  PetscInt,pointer  :: int_buffer(:,:)
+  PetscInt, pointer :: int_buffer_1d(:)
+  PetscInt, pointer :: int_buffer_2d(:,:)
   character(len=MAXSTRINGLENGTH) :: string
 
 #if defined(PETSC_HAVE_HDF5)
     call h5sselect_hyperslab_f(data_space_id,H5S_SELECT_SET_F,offset,length,hdf5_err)
   
     ! Initialize data buffer
-    allocate(int_buffer(length(1),1))
+    allocate(int_buffer_1d(length(1)))
     
     ! Create property list
     call h5pcreate_f(H5P_DATASET_XFER_F,prop_id,hdf5_err)
 #endif
   
     ! Read the dataset collectively
-    call h5dread_f(data_set_id,H5T_NATIVE_INTEGER,int_buffer,&
+    call h5dread_f(data_set_id,H5T_NATIVE_INTEGER,int_buffer_1d,&
                    dims_h5,hdf5_err,memory_space_id,data_space_id)
 
     ! allocate array to store vertices for each cell
     ! It is assumed that cell ids in the HDF5 are 1-based. Converting them to
     ! 0-based
     do ii = 1,region%num_cells
-      if(int_buffer(ii,1) < 1 ) then
+      if(int_buffer_1d(ii) < 1 ) then
         write(option%io_buffer,'("Cell ids in the HDF5 for region less than 1")')
         call printErrMsg(option)
       endif
-      region%cell_ids(ii) = int_buffer(ii,1) - 1
+      region%cell_ids(ii) = int_buffer_1d(ii) - 1
     enddo
+     call DeallocateArray(int_buffer_1d)
     
   case(2)
     !
      call h5sselect_hyperslab_f(data_space_id,H5S_SELECT_SET_F,offset,length,hdf5_err)
   
      ! Initialize data buffer
-     allocate(int_buffer(length(1),length(2)))
+     allocate(int_buffer_2d(length(1),length(2)))
   
      ! Create property list
      call h5pcreate_f(H5P_DATASET_XFER_F,prop_id,hdf5_err)
 #endif
   
      ! Read the dataset collectively
-     call h5dread_f(data_set_id,H5T_NATIVE_INTEGER,int_buffer,&
+     call h5dread_f(data_set_id,H5T_NATIVE_INTEGER,int_buffer_2d,&
           dims_h5,hdf5_err,memory_space_id,data_space_id)
      
      if (dims_h5(1) == 2) then
        region%num_verts = 0
        
        do ii = 1, region%num_cells
-         region%cell_ids(ii) = int_buffer(1,ii)
-         region%faces(ii) = int_buffer(2,ii)
+         region%cell_ids(ii) = int_buffer_2d(1,ii)
+         region%faces(ii) = int_buffer_2d(2,ii)
        enddo
      else
        !
        sideset%face_vertices = -999
   
        do ii = 1,sideset%nfaces
-        do jj = 2,int_buffer(1,ii)+1
-         sideset%face_vertices(jj-1,ii) = int_buffer(jj,ii)
+        do jj = 2,int_buffer_2d(1,ii)+1
+         sideset%face_vertices(jj-1,ii) = int_buffer_2d(jj,ii)
         enddo
        enddo
      endif
+     call DeallocateArray(int_buffer_2d)
   end select
     
   deallocate(dims_h5)
   deallocate(max_dims_h5)
-  deallocate(int_buffer)
 
   call h5pclose_f(prop_id,hdf5_err)
   call h5sclose_f(memory_space_id,hdf5_err)

File src/pflotran/output_aux.F90

View file
  • Ignore whitespace
   
   if (.not.associated(output_option)) return
   
+  call OutputVariableListDestroy(output_option%output_variable_list)
+  call OutputVariableListDestroy(output_option%aveg_output_variable_list)
+  
   deallocate(output_option)
   nullify(output_option)
   

File src/pflotran/patch.F90

View file
  • Ignore whitespace
 ! ************************************************************************** !
 subroutine PatchDestroy(patch)
 
+  use Utility_module, only : DeallocateArray
+
   implicit none
   
   type(patch_type), pointer :: patch
   
-  if (associated(patch%imat)) deallocate(patch%imat)
-  nullify(patch%imat)
-  if (associated(patch%sat_func_id)) deallocate(patch%sat_func_id)
-  nullify(patch%sat_func_id)
-  if (associated(patch%internal_velocities)) deallocate(patch%internal_velocities)
-  nullify(patch%internal_velocities)
-  if (associated(patch%boundary_velocities)) deallocate(patch%boundary_velocities)
-  nullify(patch%boundary_velocities)
-  if (associated(patch%internal_fluxes)) deallocate(patch%internal_fluxes)
-  nullify(patch%internal_fluxes)
-  if (associated(patch%boundary_fluxes)) deallocate(patch%boundary_fluxes)
-  nullify(patch%boundary_fluxes)
-  if (associated(patch%internal_tran_coefs)) deallocate(patch%internal_tran_coefs)
-  nullify(patch%internal_tran_coefs)
-  if (associated(patch%boundary_tran_coefs)) deallocate(patch%boundary_tran_coefs)
-  nullify(patch%boundary_tran_coefs)
-  if (associated(patch%ss_fluid_fluxes)) deallocate(patch%ss_fluid_fluxes)
-  nullify(patch%ss_fluid_fluxes)
+  call DeallocateArray(patch%imat)
+  call DeallocateArray(patch%sat_func_id)
+  call DeallocateArray(patch%internal_velocities)
+  call DeallocateArray(patch%boundary_velocities)
+  call DeallocateArray(patch%internal_fluxes)
+  call DeallocateArray(patch%boundary_fluxes)
+  call DeallocateArray(patch%internal_tran_coefs)
+  call DeallocateArray(patch%boundary_tran_coefs)
+  call DeallocateArray(patch%ss_fluid_fluxes)
 
   if (associated(patch%material_property_array)) &
     deallocate(patch%material_property_array)
   nullify(patch%surf_boundary_fluxes)
 #endif
 
-  call GridDestroy(patch%grid)
+  ! solely nullify grid since destroyed in discretization
+  nullify(patch%grid)
   call RegionDestroyList(patch%regions)
   call CouplerDestroyList(patch%boundary_conditions)
   call CouplerDestroyList(patch%initial_conditions)

File src/pflotran/realization.F90

View file
  • Ignore whitespace
   call TranConstraintDestroy(realization%sec_transport_constraint)
   call MassTransferDestroy(realization%mass_transfer_list)
   
+  call WaypointListDestroy(realization%waypoints)
+  
+  deallocate(realization)
+  nullify(realization)
+  
 end subroutine RealizationDestroy
 
 end module Realization_class

File src/pflotran/unstructured_grid.F90

View file
  • Ignore whitespace
   PetscInt          :: num_vertices_local
   PetscInt          :: num_vertices_local_save
   PetscInt          :: remainder
-  PetscInt,pointer  :: int_buffer(:,:)
-  PetscReal,pointer :: double_buffer(:,:)
+  PetscInt, pointer :: int_buffer(:,:)
+  PetscReal, pointer :: double_buffer(:,:)
   PetscInt, parameter :: max_nvert_per_cell = 8  
   PetscErrorCode    :: ierr
 
   
   call h5dclose_f(data_set_id, hdf5_err)
   
+  deallocate(int_buffer)
+  nullify(int_buffer)
   deallocate(dims_h5)
   deallocate(max_dims_h5)
 
   
   
   deallocate(double_buffer)
+  nullify(double_buffer)
   deallocate(dims_h5)
   deallocate(max_dims_h5)
   
   PetscInt          :: num_vertices_local
   PetscInt          :: num_vertices_local_save
   PetscInt          :: remainder
-  PetscInt,pointer  :: int_buffer(:,:)
-  PetscReal,pointer :: double_buffer(:,:)
+  PetscInt, pointer  :: int_buffer(:,:)
+  PetscReal, pointer :: double_buffer(:,:)
   PetscInt, parameter :: max_nvert_per_cell = 8  
   PetscErrorCode    :: ierr
 
   
   call h5dclose_f(data_set_id, hdf5_err)
   
+  deallocate(int_buffer)
+  nullify(int_buffer)
   deallocate(dims_h5)
   deallocate(max_dims_h5)
 
     unstructured_grid%vertices(ii)%z = double_buffer(3, ii)
   enddo
   
-  
   deallocate(double_buffer)
+  nullify(double_buffer)
   deallocate(dims_h5)
   deallocate(max_dims_h5)
   
       unstructured_grid%cell_vertices(jj-1, ii) = int_buffer(jj, ii)
     enddo
   enddo
+  deallocate(int_buffer)
+  nullify(int_buffer)
 
   ! Read Vertices
   call HDF5ReadDatasetReal2D(filename, &
     unstructured_grid%vertices(ii)%y = double_buffer(2, ii)
     unstructured_grid%vertices(ii)%z = double_buffer(3, ii)
   enddo
+  deallocate(double_buffer)
+  nullify(double_buffer)
 
   unstructured_grid%max_nvert_per_cell = max_nvert_per_cell
   unstructured_grid%nlmax = num_cells_local

File src/pflotran/unstructured_grid_aux.F90

View file
  • Ignore whitespace
                                natural_id_offset,is_scatter)
 
   use Option_module
-  use Utility_module, only: reallocateIntArray  
+  use Utility_module, only: reallocateIntArray, DeallocateArray
   
   implicit none
 
     enddo
     call VecRestoreArrayF90(elements_petsc,vec_ptr,ierr)
 
-    deallocate(int_array_pointer)
-    nullify(int_array_pointer)
     deallocate(int_array2)
     deallocate(int_array3)
     deallocate(int_array4)
     deallocate(int_array5)
   endif
+  call DeallocateArray(int_array_pointer)
 
   ugrid%nlmax = num_cells_local_new
   ugrid%num_ghost_cells = ghost_cell_count

File src/pflotran/waypoint.F90

View file
  • Ignore whitespace
   
   public :: WaypointCreate, &
             WaypointListCreate, &
+            WaypointListDestroy, &
             WaypointInsertInList, &
             WaypointDeleteFromList, &
             WaypointListFillIn, &
 
 ! ************************************************************************** !
 !
-! WaypointListDestroy: Deallocates a waypoint list
-! author: Glenn Hammond
-! date: 11/09/07
-!
-! ************************************************************************** !
-subroutine WaypointListDestroy(waypoint_list)
-
-  implicit none
-  
-  type(waypoint_list_type), pointer :: waypoint_list
-  
-  type(waypoint_type), pointer :: waypoint, prev_waypoint
-  
-  if (.not.associated(waypoint_list)) return
-
-  if (associated(waypoint_list%array)) deallocate(waypoint_list%array)
-  nullify(waypoint_list%array)
-  
-  waypoint => waypoint_list%first
-  do
-    if (.not.associated(waypoint)) exit
-    prev_waypoint => waypoint
-    waypoint => waypoint%next
-    call WaypointDestroy(prev_waypoint)
-  enddo
-  
-  nullify(waypoint_list%first)
-  nullify(waypoint_list%last)
-  deallocate(waypoint_list)
-  nullify(waypoint_list)
-  
-end subroutine WaypointListDestroy
-
-! ************************************************************************** !
-!
 ! WaypointSkipToTime: Returns a pointer to the first waypoint after time
 ! author: Glenn Hammond
 ! date: 1/03/08
  
 end subroutine WaypointPrint
 
+! ************************************************************************** !
+!
+! WaypointListDestroy: Destroys a simulation waypoint list
+! author: Glenn Hammond
+! date: 11/07/07
+!
+! ************************************************************************** !
+subroutine WaypointListDestroy(waypoint_list)
 
+  implicit none
+  
+  type(waypoint_list_type), pointer :: waypoint_list
+  
+  type(waypoint_type), pointer :: cur_waypoint, next_waypoint
+  
+  if (.not.associated(waypoint_list)) return
+  
+  cur_waypoint => waypoint_list%first
+  do
+    if (.not.associated(cur_waypoint)) exit
+    next_waypoint => cur_waypoint%next
+    call WaypointDestroy(cur_waypoint)
+    cur_waypoint => next_waypoint
+  enddo
+  
+  nullify(waypoint_list%first)
+  nullify(waypoint_list%last)
+  if (associated(waypoint_list%array)) deallocate(waypoint_list%array)
+  nullify(waypoint_list%array)
+
+  deallocate(waypoint_list)
+  nullify(waypoint_list)
+  
+end subroutine WaypointListDestroy 
 
 ! ************************************************************************** !
 !
 ! date: 11/09/07
 !
 ! ************************************************************************** !
+!geh: DO NOT make this subroutine recursive as waypoints within lists need to
+!     be destroyed without recursively destroying the remainder of the list.
 subroutine WaypointDestroy(waypoint)
 
   implicit none