title: Unknown intermittent crash due dragging and dropping in Qt
body: <p>I've been trying to find this bug for over a day now and I'm starting to go mad... I have re-written and closely examined the code for dragging and dropping to no avail.</p>
  
  <p>It occurs in the main Qt event loop a.exec() with the stack trace from valgrind shown below. It can sometimes crash on a QMutex::Lock() (I only use one QThread (main qt event loop), all other threads are contained in the backend and are safe)</p>
  
  <p>As the bug took more than an hour to show up using the mouse myself, I wrote a function that moves the mouse about pressing, releasing, clicking, double clicking. This causes the bug to occur within about ten minutes.</p>
  
  <p>I'd post some code up but their is a few thousand lines for the drag and drop stuff (it works perfectly apart from this annoying bug)</p>
  
  <p>The drag can occur in two types of item and is basically the same for both. On the mouse move event, the drag->exec() is called. </p>
  
  <p>I am using a subclassed mime data to allow it to contain what I require.</p>
  
  <p>Any help would be amazing...</p>
  
  <pre><code>Invalid read of size 8
    in main in Src/Program/main.cpp:77
  Address 0x40 is not stack'd, malloc'd or (recently) free'd  
    1: QCoreApplication::postEvent(QObject*, QEvent*, int) in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1
    2: /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.1
    3: QApplication::x11ClientMessage(QWidget*, _XEvent*, bool) in /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.1
    4: QApplication::x11ProcessEvent(_XEvent*) in /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.1
    5: /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.1
    6: g_main_context_dispatch in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3
    7: /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3
    8: g_main_context_iteration in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3
    9: QEventDispatcherGlib::processEvents(QFlags&amp;lt;QEventLoop::ProcessEventsFlag&amp;gt;) in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1
    10: /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.1
    11: QEventLoop::processEvents(QFlags&amp;lt;QEventLoop::ProcessEventsFlag&amp;gt;) in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1
    12: QEventLoop::exec(QFlags&amp;lt;QEventLoop::ProcessEventsFlag&amp;gt;) in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1
    13: QCoreApplication::exec() in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1
    14: main in 
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>  QApplication a(argc, argv);
  
      if (testing)
      {
        UnitTest::RunAllTests();
        return 0;
      }
  
      MainWindow w(debug, testing);
      w.show();
      w.InitialiseGUI();
      return a.exec();   &lt;- line 77
  </code></pre>
  
  <p>When drag object is created:</p>
  
  <pre><code>void ElementItem::mouseMoveEvent(QMouseEvent * event)
  {
    if
        (
         (event-&gt;buttons() &amp; Qt::LeftButton) &amp;&amp;
         ((event-&gt;pos() - _drag_start_position).manhattanLength() &gt; DRAG_START_DISTANCE)
         )
    {
  
      _control_and_status-&gt;_drag_control_object-&gt;NotifyDragStarted();
  
      try
      {
  
        // Ensure that if something goes wrong and old widget does not get deleted.
        // create a new drag object
        _drag = new QDrag(this);
  
        // create a custom mime type (subclass of QMimeType)
        WidgetMimeType *mimeData = new WidgetMimeType;
  
        ChainWidgetBase * base_widget = LoadElementAndWidget();
  
        // Set the widget as this object
        if (base_widget)
        {
          mimeData-&gt;_widget = (QWidget *) base_widget;
          mimeData-&gt;_delete_on_failure = true;
          mimeData-&gt;_widget_deletion_required = &amp;_widget_deletion_required;
        }
        else
        {
          delete _drag;
          _drag = NULL;
          throw GeneralException(ErrorReporting::ERROR_IN_DROPPABLE_CONTAINER, "Null widget");
        }
  
        _drag-&gt;setMimeData(mimeData);
  
        Qt::DropAction dropAction = _drag-&gt;exec();
  
      }
      catch(...)
      {
      }
  
      DragStopped();
    }
  }
  </code></pre>
  
  <p>This is one of the places where the drag object is created, there are 3 more that do different things, but use the same method</p>
  
  <p>Subclassed mime data:</p>
  
  <pre><code>#pragma once
  #include &lt;QtGui&gt;
  
  class WidgetMimeType : public QMimeData
  {
  public:
    WidgetMimeType():
      _widget(NULL),
      _delete_on_failure(false),
      _deletion_required_dummy(false),
      _widget_deletion_required(&amp;_deletion_required_dummy)
    {
    }
  
    ~WidgetMimeType()
    {
    }
  
    QWidget * _widget;
    bool _delete_on_failure;
    bool _deletion_required_dummy;
    bool * _widget_deletion_required;
  };
  </code></pre>
  
  <p>EDIT: I have added the QT source, location of the crash: </p>
  
  <pre><code>void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority)
  {
      if (receiver == 0) {
          qWarning("QCoreApplication::postEvent: Unexpected null receiver");
          delete event;
          return;
      }
  
      QThreadData * volatile * pdata = &amp;receiver-&gt;d_func()-&gt;threadData;
      QThreadData *data = *pdata; &lt;--- CRASHES HERE
      if (!data) {
          // posting during destruction? just delete the event to prevent a leak
          delete event;
          return;
      }
  
      // lock the post event mutex
      data-&gt;postEventList.mutex.lock();
  
      // if object has moved to another thread, follow it
      while (data != *pdata) {
          data-&gt;postEventList.mutex.unlock();
  
          data = *pdata;
          if (!data) {
              // posting during destruction? just delete the event to prevent a leak
              delete event;
              return;
          }
  
          data-&gt;postEventList.mutex.lock();
      }
  </code></pre>
  
  <p>The crash occurs on the line shown above, where it says &lt;---- CRASH HERE. Its is from dereferencing a pointer. Is this enough information to determine what is going on?</p>