backscrub icon indicating copy to clipboard operation
backscrub copied to clipboard

Memory leaks on program termination

Open BenBE opened this issue 3 years ago • 3 comments

Reprot from building with -flto -fsanitize=address,undefined:

=================================================================
==34557==ERROR: LeakSanitizer: detected memory leaks
                                                                                                                                                                                             
Direct leak of 3096 byte(s) in 3 object(s) allocated from:
    #0 0x7f72bb584787 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:107                                                                              
    #1 0x7f72b53ff66f  (/usr/lib/x86_64-linux-gnu/libtbb.so.2+0x2766f)

Direct leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x7f72bb584587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104                                                                                
    #1 0x7f72ba80833d in cv::Mat::create(int, int const*, int) (/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2+0x17133d)
    #2 0x7f72ba816062  (/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2+0x17f062)

Direct leak of 88 byte(s) in 1 object(s) allocated from:
    #0 0x7f72bb584587 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cc:104                                                                                
    #1 0x7f72ba80833d in cv::Mat::create(int, int const*, int) (/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2+0x17133d)
    #2 0x55a79c80456b in cv::Mat::create(int, int, int) /usr/include/opencv4/opencv2/core/mat.inl.hpp:831
    #3 0x55a79c8038dc in cv::Mat::Mat(int, int, int) /usr/include/opencv4/opencv2/core/mat.inl.hpp:460
    #4 0x55a79c905f9c in bs_maskgen_new(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, unsigned long, unsigned long, void (*)(void*, char const*), void (*)(void*), void (*)(void*), void (*)(void*), void*) /home/user/bin/deepbacksub/lib/libbackscrub.cc:276
    #5 0x55a79c82b7a4 in CalcMask::CalcMask(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, unsigned long, unsigned long) /home/user/bin/deepbacksub/app/calcmask.cc:14
    #6 0x55a79c80b796 in main /home/user/bin/deepbacksub/app/deepseg.cc:290
    #7 0x7f72b97ee082 in __libc_start_main ../csu/libc-start.c:308

Indirect leak of 987280 byte(s) in 2 object(s) allocated from:
    #0 0x7f72bb582808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144                                                                                     
    #1 0x7f72ba6fd2a8 in cv::fastMalloc(unsigned long) (/usr/lib/x86_64-linux-gnu/libopencv_core.so.4.2+0x662a8)

SUMMARY: AddressSanitizer: 990552 byte(s) leaked in 7 allocation(s).

The report is against my multipass branch, but those memory leaks should be present with both main and experimental alike, as little to no actual source changes have happened on that branch yet.

Relevant change to CMakeLists.txt to reproduce:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 851c4309..c078933f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -58,6 +58,9 @@ add_subdirectory(${TENSORFLOW}/tensorflow/lite
 
 # build backscrub code
 add_compile_definitions(DEEPSEG_VERSION=${DEEPSEG_VERSION} INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX})
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,undefined -flto")
+SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,undefined -flto")
+
 include_directories(BEFORE .)
 set(CMAKE_CXX_STANDARD 17)
 

Just compile and run. Will present a report like above on exit.

BenBE avatar Jan 24 '22 20:01 BenBE

Some quick analysis seems to suggest, that the ctx.X.deallocate() calls in bs_maskgen_delete cause the memory to be leaked. Looking at the header for cv::Mat it suggest that this function shouldn't be called and is mostly a private API.

BenBE avatar Jan 24 '22 21:01 BenBE

@BenBE is this sorted?

phlash avatar Aug 24 '22 10:08 phlash

Will have to re-check. But looks like this is currently just in one of my experimental branches.

BenBE avatar Aug 24 '22 11:08 BenBE

Currently only the following allocations are complained about:

main branch:

==2449355==ERROR: LeakSanitizer: detected memory leaks
                                                                                                                                                                                             
Direct leak of 208 byte(s) in 2 object(s) allocated from:
    #0 0x7fcc298b61c7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99                                                                                
    #1 0x7fcc2892bd78  (/lib/x86_64-linux-gnu/libopencv_core.so.4.5d+0x12bd78)

Indirect leak of 987280 byte(s) in 2 object(s) allocated from:
    #0 0x7fcc298b4867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145                                                                                    
    #1 0x7fcc28872c08 in cv::fastMalloc(unsigned long) (/lib/x86_64-linux-gnu/libopencv_core.so.4.5d+0x72c08)

SUMMARY: AddressSanitizer: 987488 byte(s) leaked in 4 allocation(s).

experimental branch:

==2453267==ERROR: LeakSanitizer: detected memory leaks
                                                                                                                                                                                             
Direct leak of 208 byte(s) in 2 object(s) allocated from:
    #0 0x7f1a2c4b61c7 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99                                                                                
    #1 0x7f1a2b32bd78  (/lib/x86_64-linux-gnu/libopencv_core.so.4.5d+0x12bd78)

Indirect leak of 987280 byte(s) in 2 object(s) allocated from:
    #0 0x7f1a2c4b4867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145                                                                                    
    #1 0x7f1a2b272c08 in cv::fastMalloc(unsigned long) (/lib/x86_64-linux-gnu/libopencv_core.so.4.5d+0x72c08)

SUMMARY: AddressSanitizer: 987488 byte(s) leaked in 4 allocation(s).

BenBE avatar Oct 17 '22 19:10 BenBE