Skip to content

Commit 1b3e619

Browse files
authored
Fix memory leak in chrom_calc. (bmad-sim#1730)
1 parent 7c77976 commit 1b3e619

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

bmad/code/chrom_calc.f90

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
! This will handle open geometry lattices. In this case, dbeta/dpz and dalpha/dpz are are
77
! taken to be what is set in the beginning element.
88
!
9+
! Note: If chrom_calc is to be repeatedly called, supplying low_E_lat, high_E_lat, low_E_orb, and/or high_E_orb will save time
10+
! since memory will not have to be repeatedly allocated.
11+
!
912
! Input:
1013
! lat -- lat_struct: Lat
1114
! delta_e -- real(rp): +/- Delta energy used for the calculation. Notice that the energy difference
@@ -35,7 +38,7 @@ subroutine chrom_calc (lat, delta_e, chrom_a, chrom_b, err_flag, &
3538

3639
type (lat_struct), target :: lat
3740
type (lat_struct), optional, target :: low_E_lat, high_E_lat
38-
type (lat_struct), target :: this_lat
41+
type (lat_struct), target :: temp_lat
3942
type (lat_struct), pointer :: lat2
4043
type (coord_struct), allocatable, optional, target :: low_E_orb(:), high_E_orb(:)
4144
type (coord_struct), optional :: orb0
@@ -53,7 +56,7 @@ subroutine chrom_calc (lat, delta_e, chrom_a, chrom_b, err_flag, &
5356
integer nt, nm, stat, ix_br, ie, i0, ix_fix
5457

5558
logical, optional, intent(out) :: err_flag
56-
logical err, used_this_lat
59+
logical err
5760

5861
character(*), parameter :: r_name = 'chrom_calc'
5962

@@ -78,14 +81,12 @@ subroutine chrom_calc (lat, delta_e, chrom_a, chrom_b, err_flag, &
7881
nt = branch%n_ele_track
7982
nm = branch%n_ele_max
8083

81-
! lower energy tune
84+
! Low energy calc.
8285

8386
if (present(low_E_lat)) then
8487
lat2 => low_E_lat
85-
used_this_lat = .false.
8688
else
87-
lat2 => this_lat
88-
used_this_lat = .true.
89+
lat2 => temp_lat
8990
endif
9091

9192
lat2 = lat
@@ -171,20 +172,17 @@ subroutine chrom_calc (lat, delta_e, chrom_a, chrom_b, err_flag, &
171172
branch%ele(i0:nm)%y%detap_dpz = branch2%ele(i0:nm)%y%etap
172173
branch%ele(i0:nm)%z%detap_dpz = branch2%ele(i0:nm)%z%etap
173174

174-
! higher energy tune
175+
! Higher energy lattice
175176

176177
if (present(high_E_lat)) then
177178
lat2 => high_E_lat
178-
used_this_lat = .false.
179179
else
180-
lat2 => this_lat
180+
lat2 => temp_lat
181181
endif
182182

183-
if (.not. used_this_lat) then
184-
lat2 = lat
185-
branch2 => lat2%branch(ix_br)
186-
call set_on_off (rfcavity$, lat2, off$, ix_branch = ix_br)
187-
endif
183+
lat2 = lat
184+
branch2 => lat2%branch(ix_br)
185+
call set_on_off (rfcavity$, lat2, off$, ix_branch = ix_br)
188186

189187
if (present(high_E_orb)) then
190188
call reallocate_coord (high_E_orb, branch%n_ele_max)
@@ -268,5 +266,6 @@ subroutine chrom_calc (lat, delta_e, chrom_a, chrom_b, err_flag, &
268266
branch%ele(i0:nm)%z%detap_dpz = (branch2%ele(i0:nm)%z%etap - branch%ele(i0:nm)%z%detap_dpz) / dE
269267

270268
if (present(err_flag)) err_flag = .false.
269+
if (.not. present(low_E_lat) .or. .not. present(high_E_lat)) call deallocate_lat_pointers(temp_lat)
271270

272271
end subroutine

bmad/doc/lat-struct.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ \section{Initializing}
6969

7070

7171
%----------------------------------------------------------------------------
72-
\section{Pointers}
72+
\section{Pointers: Allocation and Deallocation}
7373
\label{s:lat:point}
7474
\index{lat_struct!pointers}
7575

@@ -86,8 +86,8 @@ \section{Pointers}
8686
place but the pointers in \vn{lattice1} will point to different locations in physical memory so that
8787
changes to one lattice will not affect the other.
8888

89-
\Hyperref{r:deallocate.lat.pointers}{deallocate_lat_pointers} Initial allocation of the pointers in
90-
a \vn{lat_struct} variable is generally handled by the \Hyperref{r:bmad.parser}{bmad_parser} and
89+
Initial allocation of the pointers in
90+
a \vn{lat_struct} instance is generally handled by the \Hyperref{r:bmad.parser}{bmad_parser} and
9191
\Hyperref{r:lat.equal.lat}{lat_equal_lat} routines. Once allocated, local \vn{lat_struct} variables
9292
must have the save attribute or the pointers within must be appropriately deallocated before leaving
9393
the routine.

bmad/low_level/deallocate_lat_pointers.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
!+
22
! Subroutine deallocate_lat_pointers (lat)
33
!
4-
! Subroutine to deallocate the pointers in a lat.
4+
! Routine to deallocate the pointers in a lat.
5+
! This routine must be called before a lat_struct instance goes out-of-scope.
56
!
67
! Input:
78
! lat -- lat_struct: Lat with pointers.

0 commit comments

Comments
 (0)