boostorg / thread

Boost.org thread module
http://boost.org/libs/thread
198 stars 162 forks source link

Resource leak @ https://github.com/boostorg/thread/blob/boost-1.61.0/include/boost/thread/detail/thread.hpp#L859 #261

Closed venjun2000 closed 5 years ago

venjun2000 commented 5 years ago

Issue location: https://github.com/boostorg/thread/blob/boost-1.61.0/include/boost/thread/detail/thread.hpp#L859

Coverity tool detects and reports as major issue

Resource leak /DeliSpace/TegraP1Lin.Rel/External/Boost/inc/boost/thread/detail/thread.hpp at_thread_exit

viboes commented 5 years ago

Thanks for the report. Have you analyzed the report? Are you sure there is a real leak? How are you sure this function 'thread_exit_func' is not called? isn't this a false positive?

venjun2000 commented 5 years ago

@viboes , Here's the report..

840 void BOOST_THREAD_DECL make_ready_at_thread_exit(shared_ptr as); 841#endif 842 } 843 844 namespace this_thread 845 { 846 template 847 void at_thread_exit(F f) 848 { **1. alloc_fn: Storage is returned from allocation function heap_new. [show details]

  1. var_assign: Assigning: thread_exit_func = storage returned from boost::detail::heap_new(f).* 849 detail::thread_exit_function_base const thread_exit_func=detail::heap_new<detail::thread_exit_function >(f); 3. noescape: Resource thread_exit_func is not freed or pointed-to in unimplemented function add_thread_exit_function. 850 detail::add_thread_exit_function(thread_exit_func);

**CID 1588763 (#1 of 1): Resource leak (RESOURCE_LEAK)

  1. leaked_storage: Variable thread_exit_func going out of scope leaks the storage it points to.** 851 } 852 } 853}
viboes commented 5 years ago

I don't see from the report why there is a memory leak.

venjun2000 commented 5 years ago

I don't see from the report why there is a memory leak.

Can you confirm whether there is leak or is it false positive ? Is it something like the user of this library will free the storage allocated ? Is this library designed like this ?

viboes commented 5 years ago

The library free the storage at thread exit. Is Coverity checking it after the thread exits? IIUC Coverity is a static analysis tool, not a run-time one. I don't know how it can tell for sure that this function will not be called.

austin-beer commented 5 years ago

I ran valgrind --leak-check=full test_thread_exit (test_thread_exit.cpp is in the Boost.Thread test folder) against the latest develop branch and it reported that 0 bytes were leaked. The Coverity finding is a false positive. The memory is freed here:

https://github.com/boostorg/thread/blob/develop/src/pthread/thread.cpp#L104