-
Notifications
You must be signed in to change notification settings - Fork 87
After creating an new object, any remaining key-value pairs in origin json_file will be erased. #564
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I'm not seeing with with gfortran 12.3.0. I get: {
"aaa": {
"bbb": 1,
"ccc": {
"ddd": 2
},
"eee": 3,
"fff": 0.45E+1
}
}
{
"ddd": 2
}
{
"aaa": {
"bbb": 1,
"ccc": {
"ddd": 2
},
"eee": 3,
"fff": 0.45E+1
}
} I can try to test with gfortran 13 when I get a chance... |
At first I thought using
can solve the problem.
And here are the results:
So what should I do to pass parts of the json to the subroutine safely without affecting the original json_file object? |
My guess is that this issue is related to the destructor, but I don't know how to avoid it. |
Tests found that after passing the json_file object (or a certain part) to the subfunction for processing,destroying the original object (automatically by finalizer) would also raise an exception, causing the program to exit. |
The issue here is that you are extracting a pointer from one json_file, putting it into another json_file, which then gets destroyed when that variable goes out of scope. the two options you have when you do this are to make a deep copy of the pointer before putting it in the 2nd structure, or nullifying the pointer in the 2nd structure before it goes out of scope. I'll try to make an example... Option 1: subroutine test(json)
type(json_file) :: json
type(json_file) :: new_json
type(json_value), pointer :: value, value_clone
type(json_core) :: j
logical :: found
print *, "test's json get from main:"
call json%print()
call json%get("/aaa/ccc", value, found)
call j%clone(value, value_clone) ! make a deep copy of this, so we dont destroy part of the original structure when new_json goes out of scope
call new_json%initialize(path_mode=2)
call new_json%add(value_clone)
print *, "new_json before test01():"
call new_json%print()
call test01(new_json)
print *, "new_json after test01():"
call new_json%print()
print *, "json after test01():"
call json%print()
end subroutine test Option 2: subroutine test(json)
type(json_file) :: json
type(json_file) :: new_json
type(json_value), pointer :: value
logical :: found
print *, "test's json get from main:"
call json%print()
call json%get("/aaa/ccc", value, found)
call new_json%initialize(path_mode=2)
call new_json%add(value)
print *, "new_json before test01():"
call new_json%print()
call test01(new_json)
print *, "new_json after test01():"
call new_json%print()
call new_json%nullify() ! Nullify the pointer because its also in the original structure, we dont want this routine to destroy it.
print *, "json after test01():"
call json%print()
end subroutine test
|
Thanks a lot. This is the main reason! It is because of part of the pointer being destroyed in subroutine automatically that the original object can no longer be a normal object and then can no longer use
This way seems more momery-less,so I tryed this way first. And the result shows that it works well! Thank you very much again! But eventually I found that it seems that we still can't use |
Yes, if you use the |
If nullify_pointer is False, the pointer is not nullified Fixes #564
Yes, now I have learned always using By the way,it seems that |
After creating an new object of json_file type from one json_file object, any remaining key-value pairs in origin json will be erased.
Environment
gfortran 13.2.0 (Built by MSYS2 project)
fpm 0.10.0, alpha
Reproduction
Here is the
test.json
We try to get something from it in this
main.f90
and separate a portion (which could be used as an input for another function)The result shows that something in origin json_file object has been removed after a new object is created. And we can no longer get any things from it.

The text was updated successfully, but these errors were encountered: