QThreadStorage crash in Windows - 4.7.0

  • Hi,

    I am using QThreadStorage for to store thread specific data and boost threads to spawn desired number of threads. I get a strange crash in Windows [ Version 6.0.6002 ] in QThreadStorage's hasLocalData(). Below is the stack trace of the crash -

    [Frames below may be incorrect and/or missing, no symbols loaded for QtCore4.dll]

    testc.exe!QThreadStorage<TEST >::hasLocalData() Line 140 + 0x10 bytes C++
    testc.exe!test_log_stream() Line 35 + 0xc bytes C++
    testc.exe!test_thread_run() Line 51 C++
    testc.exe!boost::detail::thread_data<void (__cdecl
    )(void)>::run() Line 57 C++
    testc.exe!boost::`anonymous namespace'::thread_start_function() + 0x5b bytes C++
    msvcr90.dll!_callthreadstartex() Line 348 + 0x6 bytes C
    msvcr90.dll!_threadstartex(void * ptd=0x00d41fe0) Line 326 + 0x5 bytes C

    We are using Qt 4.7.0 built with VS 2008. Here is the below code to reproduce the crash -

    @#include <QtCore/qcoreevent.h>
    #include <boost/thread.hpp>

    #include <iostream>
    #include <sstream>
    #include <list>
    using namespace std;

    #include <stdlib.h>

    class TEST

    int range_error;

        range_error = 0;


    Q_GLOBAL_STATIC(QThreadStorage<TEST*>, tlsTEST)

    void test_log_stream()
    inst = new TEST();
    inst = tlsTEST()->localData();
    inst->range_error = 1;

    void test_thread_run( void )


    int main( int argc, char ** argv )

    int num_of_threads = 40;

    list<boost::thread *> threadList;
    for(int i = 0; i < num_of_threads; ++ i)
    threadList.push_back(new boost::thread(test_thread_run));

    list<boost::thread >::iterator it;
    for(it = threadList.begin(); it != threadList.end(); ++ it)
    t = *it;

    return 0;


    If I am doing anything wrong in the usage of tls, please let me know.


  • Any chance of upgrading to 4.7.3 to check if the issue remains in that version? It is just a patch upgrade from 4.7.0...

  • Hi Andre,

    Thanks for your reply.

    I will check in 4.7.3 as you said. Do you see any issue in the code? (Or) aware of issues in Qt 4.7.0 with tls?


  • I get the same crash even in 4.7.3.


  • This is a warning in the source of [[Doc:QThreadStorage]]:

    bq. QThreadStorage can only be used with threads started with QThread

    So, I suspect this to be the cause of your problems.
    Also, a Q(Core)Application is not running, which may cause problems too.

  • Hi,

    Thanks for your reply.

    What you mentioned was clearly mentioned as a caveat in 4.2 -


    But this is no longer mentioned in 4.7 -


    Should we not take it for granted that it works with non Qt threads?


  • [quote author="kartlee" date="1314379149"]
    Should we not take it for granted that it works with non Qt threads?

    As the warning is still in the source code, I would say no, it only works with QThread, but not with with threads started using platform-specific APIs or other libraries like boost.

Log in to reply