Memory leak working with boost::thread.


Hi all.

These days I've been trying to learn how to work with threads. I've been trying different things and I believe that the boost library is my best option because expert programmers have done all the work.

I found an example about how to work with threads in:
http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html

But when I compile that code and make a test with valgrind, valgrind says that there is a memory leak.

Could anybody help me please? I don't know what I'm doing wrong.

The code with small modifications:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp> 

void workerFunc()  
{  

    int i;
    
    boost::posix_time::seconds workTime(3); 
    
    for (i=1;i<=3;i++)
    { 
       
    std::cout << "Worker: running ..." << std::endl;  
         
    boost::this_thread::sleep(workTime);
    
    }  
       
    std::cout << "Worker: finished" << std::endl;  
} 

int main(void)
{

    std::cout << "main: startup" << std::endl;  
       
    boost::thread workerThread(workerFunc);  
       
    std::cout << "main: waiting for thread" << std::endl;  
       
    workerThread.join();  
       
    std::cout << "main: done" << std::endl;  
       
    return 0; 

}


Valgrind output:

==5009== HEAP SUMMARY:
==5009==     in use at exit: 8 bytes in 1 blocks
==5009==   total heap usage: 21 allocs, 20 frees, 1,965 bytes allocated
==5009== 
==5009== 8 bytes in 1 blocks are still reachable in loss record 1 of 1
==5009==    at 0x4023F50: malloc (vg_replace_malloc.c:236)
==5009==    by 0x4042DDB: boost::detail::get_once_per_thread_epoch() (in /home/blue/Desktop/informatica/proyectos/biblioteca/libboost_thread.so.1.51.0)
==5009==    by 0x403A5A4: T.1510 (in /home/blue/Desktop/informatica/proyectos/biblioteca/libboost_thread.so.1.51.0)
==5009==    by 0x403A706: boost::detail::get_current_thread_data() (in /home/blue/Desktop/informatica/proyectos/biblioteca/libboost_thread.so.1.51.0)
==5009==    by 0x4041654: boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*) (in /home/blue/Desktop/informatica/proyectos/biblioteca/libboost_thread.so.1.51.0)
==5009==    by 0x4041FE9: boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) (in /home/blue/Desktop/informatica/proyectos/biblioteca/libboost_thread.so.1.51.0)
==5009==    by 0x403C696: boost::thread::join() (in /home/blue/Desktop/informatica/proyectos/biblioteca/libboost_thread.so.1.51.0)
==5009==    by 0x8050F4A: ??? (in /home/blue/Desktop/informatica/proyectos/biblioteca/app)
==5009==    by 0x41C8CA5: (below main) (libc-start.c:228)
==5009== 
==5009== LEAK SUMMARY:
==5009==    definitely lost: 0 bytes in 0 blocks
==5009==    indirectly lost: 0 bytes in 0 blocks
==5009==      possibly lost: 0 bytes in 0 blocks
==5009==    still reachable: 8 bytes in 1 blocks
==5009==         suppressed: 0 bytes in 0 blocks
==5009== 
==5009== For counts of detected and suppressed errors, rerun with: -v
==5009== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 27 from 6)


Thanks a lot for your time.

PS: Boost C++ libraries Version 1.51.0
Last edited on
Looks very similar to this:

http://boost.2283326.n4.nabble.com/thread-Memory-leak-in-Boost-Thread-td2648030.html

According to that, it isn't a memory leak, it's how valgrind checks for a leak.
Thanks a lot for your answer. I couldn't find that in my searches.

I copy the answer from that post in case someone finds it useful.


Is that really a memory leak?

most likely not. the memory in question is freed by a deleter of
pthread_key_create, which means when the (main) thread is exited.
valgrind apparently does the leak checking before that.

Stefan Strasser-2


Thanks a lot for your help, it was really useful.

PS: Increasing the number of threads doesn't increase the amount of memory leaked.
Topic archived. No new replies allowed.