Boost logo

Boost Users :

From: Roland (roland.schwarz_at_[hidden])
Date: 2003-11-14 13:30:58


Dear Boosters!

As I am still trying to figure out how to use the thread local storage pointer I found another strange behaviour. Perhaps I am misunderstanding the docs, or this might be a bug.
System: VC6.0 CVS Head

Problem: When using release on a tss variable, the associated automatic cleanup routine seems not to be updated correctly as it tries to delete the original pointer, instead of the 0-pointer.

Example (simple modification of tss.cpp example):

boost::thread_specific_ptr<int> value;

void increment()
{
    int* p = value.get();
    ++*p;
}

void thread_proc()
{
    value.reset(new int(0)); // initialize the thread's storage
    for (int i=0; i<10; ++i)
    {
        increment();
        int* p = value.get();
        assert(*p == i+1);
    }
//******** added the following two lines
   int* p = value.release();
   delete p; // deleting the released pointer
//*******
}

int main(int argc, char* argv[])
{
    boost::thread_group threads;
    for (int i=0; i<5; ++i)
        threads.create_thread(&thread_proc);
    threads.join_all();
}

In the debug version the program faults at exit, as the pointers are beeing freed a second time.
I suspect the problem lies in the following function, as it does not set the cleanup handler if the value is 0

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);
}

Regards,
Roland Schwarz


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net