title: Does libtorch leak?
body: <p>I am using pytorch c++ library (libtorch) in my project. When using valgrind it seems that some memory is not free properly.</p>
  <p><strong>Main.cpp:</strong></p>
  <pre><code>int main() {
      // Do somenting with libtorch here...
      std::cout &lt;&lt; &quot;end of main&quot; &lt;&lt; std::endl;
      return EXIT_SUCCESS;
  }
  </code></pre>
  <p><strong>Valgrind command:</strong>
  <code>valgrind --leak-check=full ./myapp</code></p>
  <p><strong>Valgrind output:</strong></p>
  <pre><code>==385785== Memcheck, a memory error detector
  ==385785== Copyright (C) 2002-2017, and GNU GPL d, by Julian Seward et al.
  ==385785== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
  ==385785== Command: ./btai
  ==385785== 
  ==385785== Warning: set address range perms: large range [0x48f5000, 0x17ecd000) (defined)
  end of main
  ==385785== Conditional jump or move depends on uninitialised value(s)
  ==385785==    at 0x8181EE8: torch::jit::deregisterOperator(c10::FunctionSchema const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x8361AB5: torch::jit::(anonymous namespace)::RegistrationListener::onOperatorDeregistered(c10::OperatorHandle const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x583ACAC: c10::Dispatcher::deregisterDef_(c10::OperatorHandle const&amp;, c10::OperatorName const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x58733D4: c10::RegisterOperators::~RegisterOperators() (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x1827F15D: __cxa_finalize (cxa_finalize.c:83)
  ==385785==    by 0x574C6E2: ??? (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x4011F5A: _dl_fini (dl-fini.c:138)
  ==385785==    by 0x1827EA26: __run_exit_handlers (exit.c:108)
  ==385785==    by 0x1827EBDF: exit (exit.c:139)
  ==385785==    by 0x1825C0B9: (below main) (libc-start.c:342)
  ==385785== 
  ==385785== Conditional jump or move depends on uninitialised value(s)
  ==385785==    at 0x81820FD: torch::jit::deregisterOperator(c10::FunctionSchema const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x8361AB5: torch::jit::(anonymous namespace)::RegistrationListener::onOperatorDeregistered(c10::OperatorHandle const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x583ACAC: c10::Dispatcher::deregisterDef_(c10::OperatorHandle const&amp;, c10::OperatorName const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x58733D4: c10::RegisterOperators::~RegisterOperators() (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x1827F15D: __cxa_finalize (cxa_finalize.c:83)
  ==385785==    by 0x574C6E2: ??? (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x4011F5A: _dl_fini (dl-fini.c:138)
  ==385785==    by 0x1827EA26: __run_exit_handlers (exit.c:108)
  ==385785==    by 0x1827EBDF: exit (exit.c:139)
  ==385785==    by 0x1825C0B9: (below main) (libc-start.c:342)
  ==385785== 
  ==385785== 
  ==385785== HEAP SUMMARY:
  ==385785==     in use at exit: 724,686 bytes in 11,651 blocks
  ==385785==   total heap usage: 481,322 allocs, 469,671 frees, 59,519,540 bytes allocated
  ==385785== 
  ==385785== 256 bytes in 1 blocks are possibly lost in loss record 11,294 of 11,400
  ==385785==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==385785==    by 0xB7CB7A3: mm_account_ptr_by_tid..0 (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0xB7CAE79: mkl_serv_malloc (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x9AB5B86: mkl_serv_domain_get_max_threads (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x5791278: at::init_num_threads() (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x8E1C68B: at::native::(anonymous namespace)::min_all_kernel_impl(at::Tensor&amp;, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x5B19042: at::native::min(at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x633CD3B: c10::impl::wrap_kernel_functor_unboxed_&lt;c10::impl::detail::WrapFunctionIntoFunctor_&lt;c10::CompileTimeFunctionPointer&lt;at::Tensor (at::Tensor const&amp;), &amp;at::(anonymous namespace)::(anonymous namespace)::wrapper__min&gt;, at::Tensor, c10::guts::typelist::typelist&lt;at::Tensor const&amp;&gt; &gt;, at::Tensor (at::Tensor const&amp;)&gt;::call(c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x6154390: at::Tensor c10::Dispatcher::redispatch&lt;at::Tensor, at::Tensor const&amp;&gt;(c10::TypedOperatorHandle&lt;at::Tensor (at::Tensor const&amp;)&gt; const&amp;, c10::DispatchKeySet, at::Tensor const&amp;) const [clone .isra.165] (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x615DA3E: at::redispatch::min(c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x7AC1310: torch::autograd::VariableType::(anonymous namespace)::min(c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x7AC17EE: c10::impl::wrap_kernel_functor_unboxed_&lt;c10::impl::detail::WrapFunctionIntoFunctor_&lt;c10::CompileTimeFunctionPointer&lt;at::Tensor (c10::DispatchKeySet, at::Tensor const&amp;), &amp;torch::autograd::VariableType::(anonymous namespace)::min&gt;, at::Tensor, c10::guts::typelist::typelist&lt;c10::DispatchKeySet, at::Tensor const&amp;&gt; &gt;, at::Tensor (c10::DispatchKeySet, at::Tensor const&amp;)&gt;::call(c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785== 
  ==385785== 69,664 bytes in 1 blocks are possibly lost in loss record 11,400 of 11,400
  ==385785==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==385785==    by 0xB7CBA27: mm_account_ptr_by_tid..0 (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0xB7CAE79: mkl_serv_malloc (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x9AB5B86: mkl_serv_domain_get_max_threads (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x5791278: at::init_num_threads() (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x8E1C68B: at::native::(anonymous namespace)::min_all_kernel_impl(at::Tensor&amp;, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x5B19042: at::native::min(at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x633CD3B: c10::impl::wrap_kernel_functor_unboxed_&lt;c10::impl::detail::WrapFunctionIntoFunctor_&lt;c10::CompileTimeFunctionPointer&lt;at::Tensor (at::Tensor const&amp;), &amp;at::(anonymous namespace)::(anonymous namespace)::wrapper__min&gt;, at::Tensor, c10::guts::typelist::typelist&lt;at::Tensor const&amp;&gt; &gt;, at::Tensor (at::Tensor const&amp;)&gt;::call(c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x6154390: at::Tensor c10::Dispatcher::redispatch&lt;at::Tensor, at::Tensor const&amp;&gt;(c10::TypedOperatorHandle&lt;at::Tensor (at::Tensor const&amp;)&gt; const&amp;, c10::DispatchKeySet, at::Tensor const&amp;) const [clone .isra.165] (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x615DA3E: at::redispatch::min(c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x7AC1310: torch::autograd::VariableType::(anonymous namespace)::min(c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785==    by 0x7AC17EE: c10::impl::wrap_kernel_functor_unboxed_&lt;c10::impl::detail::WrapFunctionIntoFunctor_&lt;c10::CompileTimeFunctionPointer&lt;at::Tensor (c10::DispatchKeySet, at::Tensor const&amp;), &amp;torch::autograd::VariableType::(anonymous namespace)::min&gt;, at::Tensor, c10::guts::typelist::typelist&lt;c10::DispatchKeySet, at::Tensor const&amp;&gt; &gt;, at::Tensor (c10::DispatchKeySet, at::Tensor const&amp;)&gt;::call(c10::OperatorKernel*, c10::DispatchKeySet, at::Tensor const&amp;) (in /home/tmac3/Desktop/Branching_Time_Active_Inference/libs/torch/lib/libtorch_cpu.so)
  ==385785== 
  ==385785== LEAK SUMMARY:
  ==385785==    definitely lost: 0 bytes in 0 blocks
  ==385785==    indirectly lost: 0 bytes in 0 blocks
  ==385785==      possibly lost: 69,920 bytes in 2 blocks
  ==385785==    still reachable: 654,766 bytes in 11,649 blocks
  ==385785==                       of which reachable via heuristic:
  ==385785==                         stdstring          : 359,526 bytes in 4,879 blocks
  ==385785==         suppressed: 0 bytes in 0 blocks
  ==385785== Reachable blocks (those to which a pointer was found) are not shown.
  ==385785== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  ==385785== 
  ==385785== Use --track-origins=yes to see where uninitialised values come from
  ==385785== For lists of detected and suppressed errors, rerun with: -s
  ==385785== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
  </code></pre>
  <p>As you can see there are valgrind errors and memory leaks after the main function. They seems to come from libtorch, where does those errors comes from, and how to get rid of them?</p>