ufs-community / UFS_UTILS

Utilities for the NCEP models.
Other
21 stars 108 forks source link

memory leak in cghres_cube test ftst_sfc_input_data #489

Closed edwardhartnett closed 3 years ago

edwardhartnett commented 3 years ago

When adding the address santitizer run I find some problems in ftst_sfc_input_data()

(base) ed@mikado:~/UFS_UTILS/b/tests/chgres_cube$ ./ftst_sfc_input_data 
 Starting test of check_soilt subroutine.
 SUCCESS!

=================================================================
==22968==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x7f0d5a117bc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #1 0x5578c065d3f2 in test_sfc_input_data /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:33
    #2 0x5578c065cbf8 in main /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:8
    #3 0x7f0d58ddecb1 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28cb1)

Direct leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x7f0d5a117bc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #1 0x5578c065d061 in test_sfc_input_data /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:31
    #2 0x5578c065cbf8 in main /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:8
    #3 0x7f0d58ddecb1 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28cb1)

Direct leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x7f0d5a117bc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #1 0x5578c065d142 in test_sfc_input_data /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:32
    #2 0x5578c065cbf8 in main /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:8
    #3 0x7f0d58ddecb1 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28cb1)

Direct leak of 72 byte(s) in 1 object(s) allocated from:
    #0 0x7f0d5a117bc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #1 0x5578c065d039 in test_sfc_input_data /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:30
    #2 0x5578c065cbf8 in main /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:8
    #3 0x7f0d58ddecb1 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28cb1)

Direct leak of 36 byte(s) in 1 object(s) allocated from:
    #0 0x7f0d5a117bc8 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
    #1 0x5578c065d01f in test_sfc_input_data /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:29
    #2 0x5578c065cbf8 in main /home/ed/UFS_UTILS/tests/chgres_cube/ftst_sfc_input_data.F90:8
    #3 0x7f0d58ddecb1 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28cb1)

SUMMARY: AddressSanitizer: 540 byte(s) leaked in 5 allocation(s).

There are some allocated vars in this test that are not deallocated. I have added the deallocate and will put up a PR shortly.

@LarissaReames-NOAA note that any memory allocated in a test has to be deallocated. In this way we can test for memory leaks in the library.

LarissaReames-NOAA commented 3 years ago

@edwardhartnett Thanks for finding and fixing that. I thought I had made sure to deallocate arrays where necessary, but my brain clearly forgot some that I added as I got the code to work. I'll do my best to make sure they all get deallocated in future tests.

edwardhartnett commented 3 years ago

@LarissaReames-NOAA you won't have to count on your memory. ;-) We are adding checking for this to the CI system, which is how I found this leak.

After we merge #488, PRs will not pass CI if they contain any memory issues.

LarissaReames-NOAA commented 3 years ago

@edwardhartnett I saw that, and it will definitely help. FWIW, in the feature/check_canopy PR I did add in deallocate statements for the check_soilt test when adding in the check_canopy test. So that PR should pass the memory test.

edwardhartnett commented 3 years ago

BTW to run these checks on the desktop, do a clean build with GNU with this CMake option:

-DCMAKE_Fortran_FLAGS="-fsanitize=address -fno-omit-frame-pointer"

Then run tests as usual. Memory issues will cause the test to error out.