Skip to content

Commit b47871a

Browse files
committed
Merge pull request #77 from jacobwilliams/stack-overflow
added fix for stack overflow which can occur for very large structures. ...
2 parents ac9dabb + b51d5d0 commit b47871a

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/json_module.f90

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,22 +1571,40 @@ end subroutine json_value_create
15711571
!
15721572
! SOURCE
15731573

1574-
recursive subroutine json_value_destroy(me)
1574+
recursive subroutine json_value_destroy(me,destroy_next)
15751575

15761576
implicit none
15771577

15781578
type(json_value),pointer :: me
1579+
logical(LK),intent(in),optional :: destroy_next !if true, then me%next is also destroyed (default is true)
1580+
1581+
logical(LK) :: des_next
1582+
type(json_value), pointer :: p
15791583

15801584
if (associated(me)) then
15811585

1586+
if (present(destroy_next)) then
1587+
des_next = destroy_next
1588+
else
1589+
des_next = .true.
1590+
end if
1591+
15821592
if (allocated(me%name)) deallocate(me%name)
15831593

15841594
call destroy_json_data(me)
15851595

1586-
if (associated(me%children)) call json_value_destroy(me%children)
1587-
me%n_children = 0
1596+
if (associated(me%children)) then
1597+
do while (me%n_children > 0)
1598+
p => me%children
1599+
me%children => me%children%next
1600+
me%n_children = me%n_children - 1
1601+
call json_value_destroy(p,.false.)
1602+
end do
1603+
nullify(me%children)
1604+
nullify(p)
1605+
end if
15881606

1589-
if (associated(me%next)) call json_value_destroy(me%next)
1607+
if (associated(me%next) .and. des_next) call json_value_destroy(me%next)
15901608

15911609
if (associated(me%previous)) nullify(me%previous)
15921610
if (associated(me%parent)) nullify(me%parent)

0 commit comments

Comments
 (0)