<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Destructor not called when returning object from template class method"
   href="https://bugs.llvm.org/show_bug.cgi?id=47418">47418</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Destructor not called when returning object from template class method
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>tavplubix@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23928" name="attach_23928" title="minimized code to reproduce a bug, version 2">attachment 23928</a> <a href="attachment.cgi?id=23928&action=edit" title="minimized code to reproduce a bug, version 2">[details]</a></span>
minimized code to reproduce a bug, version 2

LeakSanitizer and valgrind report a strange memory leak when building the
following code with clang. No leaks are found when building it with gcc. Seems
like it's a bug in clang and some destructor is not called.

Original code: <a href="https://github.com/ClickHouse/ClickHouse/pull/14295">https://github.com/ClickHouse/ClickHouse/pull/14295</a>

See attached file with minimized code.

How to reproduce:

$ clang++-10 -v
clang version 10.0.0-4ubuntu1~18.04.2 
Target: x86_64-pc-linux-gnu
...
$ clang++-10 -std=c++17 -fsanitize=address minimized2.cpp
$ ./a.out 
0
ctor 7ffe1e6d8920
copy 603000000010
move 603000000040
dtor 603000000010
dtor 7ffe1e6d8920
dtor 603000000040
1
ctor 7ffe1e6d8920
copy 603000000070
move 6030000000a0
dtor 7ffe1e6d8920
dtor 6030000000a0
2
ctor 7ffe1e6d8920
copy 6030000000d0
move 603000000100
dtor 6030000000d0
dtor 7ffe1e6d8920
dtor 603000000100
3

=================================================================
==3408==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x4c43dd in operator new(unsigned long)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c43dd)
    #1 0x4ca50b in __gnu_cxx::new_allocator<Elem>::allocate(unsigned long, void
const*) (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca50b)
    #2 0x4ca4b3 in std::allocator_traits<std::allocator<Elem>
<span class="quote">>::allocate(std::allocator<Elem>&, unsigned long)</span >
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca4b3)
    #3 0x4ca3f2 in std::_Vector_base<Elem, std::allocator<Elem>
<span class="quote">>::_M_allocate(unsigned long)</span >
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca3f2)
    #4 0x4c9e6f in void std::vector<Elem, std::allocator<Elem>
<span class="quote">>::_M_range_initialize<Elem const*>(Elem const*, Elem const*,</span >
std::forward_iterator_tag) (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c9e6f)
    #5 0x4c7f09 in std::vector<Elem, std::allocator<Elem>
<span class="quote">>::vector(std::initializer_list<Elem>, std::allocator<Elem> const&)</span >
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c7f09)
    #6 0x4ccb51 in TableFunctionTemplate<false>::getActualTableStructure()
const (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ccb51)
    #7 0x4c7028 in main (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c7028)
    #8 0x7f4eae5d5b96 in __libc_start_main
/build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310

Indirect leak of 42 byte(s) in 1 object(s) allocated from:
    #0 0x4c43dd in operator new(unsigned long)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c43dd)
    #1 0x4c949b in __gnu_cxx::new_allocator<char>::allocate(unsigned long, void
const*) (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c949b)
    #2 0x4c93e3 in std::allocator_traits<std::allocator<char>
<span class="quote">>::allocate(std::allocator<char>&, unsigned long)</span >
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c93e3)
    #3 0x4c8ffb in std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::_M_create(unsigned long&, unsigned long)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c8ffb)
    #4 0x4c8a53 in void std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char
const*, char const*, std::forward_iterator_tag)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c8a53)
    #5 0x4c884b in void std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >::_M_construct_aux<char
const*>(char const*, char const*, std::__false_type)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c884b)
    #6 0x4c85f4 in void std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char
const*, char const*) (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c85f4)
    #7 0x4c8475 in std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string<std::allocator<char> >(char const*,
std::allocator<char> const&)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c8475)
    #8 0x4ca819 in Print::Print(Print const&)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca819)
    #9 0x4ca6fc in Elem::Elem(Elem const&)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca6fc)
    #10 0x4ca67c in void std::_Construct<Elem, Elem const&>(Elem*, Elem const&)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca67c)
    #11 0x4ca5ae in Elem* std::__uninitialized_copy<false>::__uninit_copy<Elem
const*, Elem*>(Elem const*, Elem const*, Elem*)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca5ae)
    #12 0x4ca568 in Elem* std::uninitialized_copy<Elem const*, Elem*>(Elem
const*, Elem const*, Elem*) (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca568)
    #13 0x4ca448 in Elem* std::__uninitialized_copy_a<Elem const*, Elem*,
Elem>(Elem const*, Elem const*, Elem*, std::allocator<Elem>&)
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ca448)
    #14 0x4c9f76 in void std::vector<Elem, std::allocator<Elem>
<span class="quote">>::_M_range_initialize<Elem const*>(Elem const*, Elem const*,</span >
std::forward_iterator_tag) (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c9f76)
    #15 0x4c7f09 in std::vector<Elem, std::allocator<Elem>
<span class="quote">>::vector(std::initializer_list<Elem>, std::allocator<Elem> const&)</span >
(/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c7f09)
    #16 0x4ccb51 in TableFunctionTemplate<false>::getActualTableStructure()
const (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4ccb51)
    #17 0x4c7028 in main (/home/avtokmakov/ch/llvm-bugreport/a.out+0x4c7028)
    #18 0x7f4eae5d5b96 in __libc_start_main
/build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310

SUMMARY: AddressSanitizer: 74 byte(s) leaked in 2 allocation(s).



As you can see from the output, destructor for 603000000070 is not called in
the second case (when calling tf2.getActualTableStructure()).

Also it reproduces with clang 9 and clang trunk: <a href="https://godbolt.org/z/491eG3">https://godbolt.org/z/491eG3</a></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>