To avoid memory leaks, you need to: join working thread and make sure that it is destroyed / deleted (let it go out of scope for the std::threads allocated in the stack or explicitly call delete for std::thread* ).
See thread :: ~ thread in cppreference:
A stream object does not have an associated stream (and is safe to destroy it) after:
- it was built by default
- he was moved from
- join () was called
- Disabled () was called
An unbound stream, therefore, cannot be safely destroyed.
A join() ed std::thread will still occupy some memory. Therefore, you need to make sure that it is properly freed if it is on the heap.
source share