jacobwilliams / json-fortran

A Modern Fortran JSON API
https://jacobwilliams.github.io/json-fortran/
Other
333 stars 82 forks source link

json_file assignment memory leak #567

Open jacobwilliams opened 2 months ago

jacobwilliams commented 2 months ago

See #563. Valgrind is showing a memory leak for code with the form f = json_file(...). It is not clear if this is a compiler bug in gfortran or some fundamental issue.

Example:

program main

    use json_module 
    implicit none

    call test()

    contains 

    subroutine test()

        type(json_core) :: json
        type(json_file) :: my_file
        type(json_value), pointer :: root

        call json%create_object(root,'')
        my_file = json_file(root)  ! <-- memory leak

    end subroutine test

end program main

Valgrind (fpm test jf_test_12 --runner "valgrind --leak-check=full --show-leak-kinds=all") gives:

==132310== Memcheck, a memory error detector
==132310== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==132310== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==132310== Command: build/gfortran_E167FD2A985B468F/test/jf_test_12
==132310== 
==132310== 
==132310== HEAP SUMMARY:
==132310==     in use at exit: 185 bytes in 2 blocks
==132310==   total heap usage: 61 allocs, 59 frees, 20,596 bytes allocated
==132310== 
==132310== 1 bytes in 1 blocks are indirectly lost in loss record 1 of 2
==132310==    at 0x48761B8: malloc (vg_replace_malloc.c:442)
==132310==    by 0x139CDF: __json_value_module_MOD_json_value_rename (json_value_module.F90:1946)
==132310==    by 0x10FF17: __json_value_module_MOD_to_object (json_value_module.F90:10850)
==132310==    by 0x110E0B: __json_value_module_MOD_json_value_create_object (json_value_module.F90:10582)
==132310==    by 0x10B857: test.0 (jf_test_12.F90:34)
==132310==    by 0x10BA5B: MAIN__ (jf_test_12.F90:25)
==132310==    by 0x10BA9B: main (jf_test_12.F90:23)
==132310== 
==132310== 185 (184 direct, 1 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2
==132310==    at 0x48761B8: malloc (vg_replace_malloc.c:442)
==132310==    by 0x1390CB: __json_value_module_MOD_json_value_create (json_value_module.F90:2211)
==132310==    by 0x110DEB: __json_value_module_MOD_json_value_create_object (json_value_module.F90:10581)
==132310==    by 0x10B857: test.0 (jf_test_12.F90:34)
==132310==    by 0x10BA5B: MAIN__ (jf_test_12.F90:25)
==132310==    by 0x10BA9B: main (jf_test_12.F90:23)
==132310== 
==132310== LEAK SUMMARY:
==132310==    definitely lost: 184 bytes in 1 blocks
==132310==    indirectly lost: 1 bytes in 1 blocks
==132310==      possibly lost: 0 bytes in 0 blocks
==132310==    still reachable: 0 bytes in 0 blocks
==132310==         suppressed: 0 bytes in 0 blocks
==132310== 
==132310== For lists of detected and suppressed errors, rerun with: -s
==132310== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
jacobwilliams commented 2 months ago

See this gfortran ticket: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80524

Might be the same issue? I'm only testing up to gfortran 12.3 (from 2022)