Skip to content

Commit 08fe9c3

Browse files
Merge pull request #566 from jacobwilliams/564-nullify-option
add a new option to json_file constructor
2 parents 9299ad5 + baef6df commit 08fe9c3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/json_file_module.F90

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ end subroutine get_json_core_in_file
569569

570570
function initialize_json_file(p,&
571571
#include "json_initialize_dummy_arguments.inc"
572-
) result(file_object)
572+
, nullify_pointer &
573+
) result(file_object)
573574

574575
implicit none
575576

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

582590
call file_object%initialize(&
583591
#include "json_initialize_dummy_arguments.inc"
@@ -588,7 +596,11 @@ function initialize_json_file(p,&
588596
! we have to nullify it to avoid
589597
! a dangling pointer when the file
590598
! goes out of scope
591-
nullify(p)
599+
if (present(nullify_pointer)) then
600+
if (nullify_pointer) nullify(p)
601+
else
602+
nullify(p)
603+
end if
592604
end if
593605

594606
end function initialize_json_file

0 commit comments

Comments
 (0)