Boost logo

Boost :

From: Oscar Ciurana (oscar_at_[hidden])
Date: 2003-08-08 02:17:21


Hi,

I'm working with boost::thread_specific_ptr in a Win32 environment and
sometimes I get an access violation
when my program exits. To reproduce the error I write the following
sample program:

...
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>
#include <iostream>

class Data
{
public:
 ~Data()
 {
  std::cout << reinterpret_cast<int>(this) << std::endl ;
 }
} ;

typedef boost::thread_specific_ptr<Data> ThrData ;

void main()
{
 ThrData data ;
 data.reset( new Data ) ;
 data.reset() ;
}
...

You can see how the Data destructor is called twice, one in the
data.reset() execution and the second when the program
exits.

I was examining the boost source code and I think the problem is in the
file libs\thread\src\tss.cpp
...
bool tss::set(void* value)
{
    if (value && m_cleanup)
    {
        cleanup_handlers* handlers = get_handlers();
        assert(handlers);
        if (!handlers)
            return false;
        cleanup_info info(m_cleanup, value);
        (*handlers)[m_key] = info;
    }
    return !!TlsSetValue(m_key, value);
}
...
In this function when value is a NULL pointer the cleanup_info data is
not actualized. So when the thread ends
it will delete the data that was deleted previously with the
boost::thread_specific_ptr::reset() method.

I think the solution is
...
bool tss::set(void* value)
{
    if (m_cleanup)
    {
....
This way a NULL pointer actualizes the cleanup_info data, but I don't
know if there is any lateral effect when removing
value from the 'if' sentence.

Regards,
Oscar.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk