diff --git a/src/json_file_module.F90 b/src/json_file_module.F90 index 43bfc220d..c48d18993 100644 --- a/src/json_file_module.F90 +++ b/src/json_file_module.F90 @@ -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 @@ -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" @@ -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