From 764f89b26077a7da30d60a34e73f5ed80bfd11b4 Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Sat, 1 Jun 2024 15:49:15 -0500 Subject: [PATCH 1/2] add a new option to json_file constructor If nullify_pointer is False, the pointer is not nullified Fixes #564 --- src/json_file_module.F90 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/json_file_module.F90 b/src/json_file_module.F90 index 43bfc220d7..7b89e1342c 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,8 @@ 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) call file_object%initialize(& #include "json_initialize_dummy_arguments.inc" @@ -588,7 +591,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 From baef6dfb0c52463186d6df14f3ab26932129220c Mon Sep 17 00:00:00 2001 From: Jacob Williams Date: Mon, 10 Jun 2024 17:38:06 -0500 Subject: [PATCH 2/2] docstring update --- src/json_file_module.F90 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/json_file_module.F90 b/src/json_file_module.F90 index 7b89e1342c..c48d18993f 100644 --- a/src/json_file_module.F90 +++ b/src/json_file_module.F90 @@ -580,7 +580,12 @@ function initialize_json_file(p,& !! 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) + !! 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"