Skip to content

Commit

Permalink
Merge pull request #566 from jacobwilliams/564-nullify-option
Browse files Browse the repository at this point in the history
add a new option to json_file constructor
  • Loading branch information
jacobwilliams authored Jun 11, 2024
2 parents 9299ad5 + baef6df commit 08fe9c3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/json_file_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ end subroutine get_json_core_in_file

function initialize_json_file(p,&
#include "json_initialize_dummy_arguments.inc"
) result(file_object)
, nullify_pointer &
) result(file_object)

implicit none

Expand All @@ -578,6 +579,13 @@ function initialize_json_file(p,&
!! as a `json_file` object. This
!! will be nullified.
#include "json_initialize_arguments.inc"
logical(LK),intent(in),optional :: nullify_pointer !! if True, then `p` will be nullified
!! if present. (default is True). Normally,
!! this should be done, because the [[json_file]] will destroy
!! the pointer when the class goes out of scope (causing `p` to be
!! a dangling pointer). However, if the intent is to use `p` in
!! a [[json_file]] and then call [[json_file:nullify]] and continue
!! to use `p`, then this should be set to False.

call file_object%initialize(&
#include "json_initialize_dummy_arguments.inc"
Expand All @@ -588,7 +596,11 @@ function initialize_json_file(p,&
! we have to nullify it to avoid
! a dangling pointer when the file
! goes out of scope
nullify(p)
if (present(nullify_pointer)) then
if (nullify_pointer) nullify(p)
else
nullify(p)
end if
end if

end function initialize_json_file
Expand Down

0 comments on commit 08fe9c3

Please sign in to comment.