https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83029

Paul Pluzhnikov <ppluzhnikov at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppluzhnikov at google dot com

--- Comment #2 from Paul Pluzhnikov <ppluzhnikov at google dot com> ---
We just hit the same bug. Confirmed in 9.0.1 20190216 (experimental) -- the
latest trunk build I have.

Trivial test case (similar to the original, but makes it easy to tell what's
going on).

------------ cut ------------
#include <iostream>
#include <memory>
#include <thread>

class Box {
 public:
  explicit Box(const char *s) : s_(s) {}
  ~Box() {
    std::cout << s_ << std::endl;
  }

 private:
  const char *const s_;
};

int main() {
  thread_local std::unique_ptr<Box> box;
  std::cout << "line " << __LINE__ << ": " << &box << std::endl;

  box.reset(new Box("Main thread"));

  std::thread thread0([&] {
    std::cout << "line " << __LINE__ << ": " << &box << std::endl;

    std::thread thread1([&] {
      std::cout << "line " << __LINE__ << ": " << &box << std::endl;
      box.reset(new Box("thread1a"));
      box.reset(new Box("thread1b"));
    });
    thread1.join();

    box.reset(new Box("thread0A"));
    box.reset(new Box("thread0B"));
  });
  thread0.join();

  std::cout << "END" << std::endl;
}
------------ cut ------------

g++ -g -fsanitize=leak -pthread thr.cc && ./a.out

line 18: 0x7f95ae9e02f0
line 23: 0x7f95abeff6f0
line 26: 0x7f95ab6fe6f0
thread1a
thread0A
END
Main thread

=================================================================
==207583==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f95ad954840 in operator new(unsigned long)
(/usr/lib/x86_64-linux-gnu/liblsan.so.0+0xf840)
    #1 0x558bd33cb127 in operator() /tmp/thr.cc:28
    #2 0x558bd33cb6cc in __invoke_impl<void, main()::<lambda()>::<lambda()> >
/usr/include/c++/7/bits/invoke.h:60
    #3 0x558bd33cb411 in __invoke<main()::<lambda()>::<lambda()> >
/usr/include/c++/7/bits/invoke.h:95
    #4 0x558bd33cbc5d in _M_invoke<0> /usr/include/c++/7/thread:234
    #5 0x558bd33cbc02 in operator() /usr/include/c++/7/thread:243
    #6 0x558bd33cbbbd in _M_run /usr/include/c++/7/thread:186
    #7 0x7f95ad67cb22  (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xbcb22)

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f95ad954840 in operator new(unsigned long)
(/usr/lib/x86_64-linux-gnu/liblsan.so.0+0xf840)
    #1 0x558bd33cb228 in operator() /tmp/thr.cc:33
    #2 0x558bd33cb864 in __invoke_impl<void, main()::<lambda()> >
/usr/include/c++/7/bits/invoke.h:60
    #3 0x558bd33cb4e2 in __invoke<main()::<lambda()> >
/usr/include/c++/7/bits/invoke.h:95
    #4 0x558bd33cbc31 in _M_invoke<0> /usr/include/c++/7/thread:234
    #5 0x558bd33cbbde in operator() /usr/include/c++/7/thread:243
    #6 0x558bd33cbb9d in _M_run /usr/include/c++/7/thread:186
    #7 0x7f95ad67cb22  (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xbcb22)

SUMMARY: LeakSanitizer: 16 byte(s) leaked in 2 allocation(s).

Here we can observe that:

1. there are 3 separate instances of std::unique_ptr<Box> (as expected), but
2. only 1 of these instances is destructed, the other two are leaked.

Reply via email to