title: Cannot lock Qt mutex (QReadWriteLock) Access violation writing
body: <p>Some background for this question is my previous question: 
  <a href="https://stackoverflow.com/questions/13129372/non-member-function-pointer-as-a-callback-in-api-to-member-function">non-member function pointer as a callback in API to member function</a> (it may well be irrelevant).</p>
  
  <p>The callback launches a thread that writes some data. There is another thread that reads the same data, and that results in some crashes.
  I just took a crash course in multi-threading (thanks SO), and here is my attempt to guarantee that the data isn't accessed by the writer and the reader at the same time. I'm using some mutex mechanism from Qt (QReadWriteLock).</p>
  
  <pre><code>#include &lt;QSharedPointer&gt;
  #include &lt;QReadWriteLock&gt;
  
  Class MyClass
  {
  public:
    MyClass();
    bool open();
    float getData();
    void streamCB(void* userdata);
  
  protected:
    float private_data_;
    QSharedPointer&lt;QReadWriteLock&gt; lock_;
  };
  
  // callback wrapper from non-member C API to member function void
  __stdcall streamCBWrapper(void* userdata)
  {
     static_cast&lt;MyClass*&gt;(userdata)-&gt;streamCB(userdata);
  }
  
  // constructor
  MyClass::MyClass()
  {
    lock_ = QSharedPointer&lt;QReadWriteLock&gt;(new QReadWriteLock());
    lock_-&gt;lockForWrite();
    private_data_ = getData();
    lock_-&gt;unlock();
  }
  
  // member callback
  void MyClass:streamCB(void* userdata)
  {
    float a = getData(); 
    lock_-&gt;lockForWrite(); //*** fails here
    private_data_ = a;
    lock_-&gt;unlock();
  }
  </code></pre>
  
  <p>I have a segmentation fault while running the program. The VS debugger says <code>Access violation writing location 0x00fedbed.</code> on the line that I marked <code>//*** fails here.</code>
  The lock worked in the constructor, but not in the callback.</p>
  
  <p>Any idea what goes wrong? What should I look at? (and how can I refine my question)
  Thanks!</p>
  
  <p>Other relevant thread 
  <a href="https://stackoverflow.com/questions/9507604/cannot-access-private-member-declared-in-class-qreadwritelockerror1error-c22">Cannot access private member declared in class 'QReadWriteLock'Error   1   error C2248: 'QReadWriteLock::QReadWriteLock'</a> (I used the QSharedPointer suggestion)</p>
  
  <p><strong>Edit 1:</strong>
  The callback is set up </p>
  
  <pre><code>bool MyClass::open()
  {
    // stuffs
    int mid = 0;
    set_stream_callback(&amp;streamCBWrapper, &amp;mid);
    // more stuffs
    return true;
  }
  </code></pre>
  
  <p><strong>Edit 2:</strong>
  Thank you for all the suggestions.
  So my mistake(s) may not be due at all to the mutex, but to my lack of understanding of the API? I'm quite confused.. Here is the API doc for the set_stream_callback.</p>
  
  <pre><code>typedef void (__stdcall *STREAM_CALLBACK)(void *userdata);
  
  /*! @brief Register a callback to be invoked when all subscribed fields have been updated
   *
   *  @param streamCB pointer to callback function
   *  @param userdata pointer to private data to be passed back as argument to callback
   *  @return 0 if successful, error code otherwise
   */
  __declspec(dllimport) int __stdcall set_stream_callback(
      STREAM_CALLBACK streamCB, void *userdata);
  </code></pre>