Crash in QTreeWidget / QTreeView index mapping on Mac OSX 10.10

This crash took me about 40hr+ to replicate and find a way how to eliminate it (unfortunately not to fix it). This crash occurred only on OS X 10.10, not 10.9, not Linux, not Windows.

2015-05-18_0850

Bug occurred in slightly modified QtTreePropertyBrowser where we have additional buttons for each property.

2015-05-18_0847

When any of these buttons caused property tree refresh, application randomly crashed. But when these actions were executed by buttons outside of the property tree, everything worked fine.

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   org.qt-project.QtWidgets      	0x0000000103f49290 QTreeModel::index(QTreeWidgetItem const*, int) const + 176
1   org.qt-project.QtWidgets      	0x0000000103f4950b QTreeModel::parent(QModelIndex const&) const + 75
2   org.qt-project.QtWidgets      	0x0000000103f23bb7 QTreeView::isIndexHidden(QModelIndex const&) const + 71
3   org.qt-project.QtWidgets      	0x0000000103f17e0d QTreeView::visualRect(QModelIndex const&) const + 93
4   org.qt-project.QtWidgets      	0x0000000103ec490d QAccessibleTableCell::rect() const + 29
5   org.qt-project.QtWidgets      	0x0000000103ec46f2 QAccessibleTableCell::state() const + 146
6   libqcocoa.dylib               	0x00000001071913aa QCocoaAccessible::hasValueAttribute(QAccessibleInterface*) + 58
7   libqcocoa.dylib               	0x000000010718df7e -[QMacAccessibilityElement accessibilityAttributeNames] + 398
8   com.apple.AppKit              	0x00007fff95dea26e NSAccessibilityEntryPointAttributeNames + 115
9   com.apple.AppKit              	0x00007fff95e275af -[NSObject(NSAccessibilityInternal) _accessibilityAttributeNamesClientError:] + 56
10  com.apple.AppKit              	0x00007fff95e2a62a CopyAttributeNames + 216
11  com.apple.HIServices          	0x00007fff9768cbce _AXXMIGCopyAttributeNames + 252
12  com.apple.HIServices          	0x00007fff976950c6 _XCopyAttributeNames + 385
13  com.apple.HIServices          	0x00007fff97671109 mshMIGPerform + 199
14  com.apple.CoreFoundation      	0x00007fff99259c79 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
15  com.apple.CoreFoundation      	0x00007fff99259beb __CFRunLoopDoSource1 + 475
16  com.apple.CoreFoundation      	0x00007fff9924b767 __CFRunLoopRun + 2375
17  com.apple.CoreFoundation      	0x00007fff9924abd8 CFRunLoopRunSpecific + 296
18  com.apple.HIToolbox           	0x00007fff9695d56f RunCurrentEventLoopInMode + 235
19  com.apple.HIToolbox           	0x00007fff9695d2ea ReceiveNextEventCommon + 431
20  com.apple.HIToolbox           	0x00007fff9695d12b _BlockUntilNextEventMatchingListInModeWithFilter + 71
21  com.apple.AppKit              	0x00007fff95a489bb _DPSNextEvent + 978
22  com.apple.AppKit              	0x00007fff95a47f68 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
23  libqcocoa.dylib               	0x000000010717b29d QCocoaEventDispatcher::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) + 2093
24  org.qt-project.QtCore         	0x0000000104b0337d QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) + 381
25  org.qt-project.QtWidgets      	0x0000000103e6c192 QDialog::exec() + 514

I updated methods from signals-slots mechanism to QEvent system. Unfortunately this didn’t help as well. Next attempt was QTimer usage instead of QEvent or signal-slot to simulate real click. But also without any success.

0   org.qt-project.QtWidgets      	0x000000011112f62e QTreeModel::data(QModelIndex const&, int) const + 46
1   org.qt-project.QtWidgets      	0x00000001110aa7bb QAccessibleTableCell::state() const + 347
2   libqcocoa.dylib               	0x00000001143913aa QCocoaAccessible::hasValueAttribute(QAccessibleInterface*) + 58
3   libqcocoa.dylib               	0x000000011438df7e -[QMacAccessibilityElement accessibilityAttributeNames] + 398
4   com.apple.AppKit              	0x00007fff95dea26e NSAccessibilityEntryPointAttributeNames + 115
5   com.apple.AppKit              	0x00007fff95e275af -[NSObject(NSAccessibilityInternal) _accessibilityAttributeNamesClientError:] + 56

With a lot of luck I finally found the reason for the crash. It’s caused by erasing a property tree (and inner QTreeWidget) together with an active focus on this widget. Unfortunately I’m not able to fix it in the QtCore because according to callstack there is only some QModelIndex manipulation without any sight of what’s going wrong.

2015-05-18_0853

The effective solution how to fix this wrong behaviour is to temporally set focus to another widget (or parent), clear the tree and set focus back to property tree. Unfortunately this solution doesn’t work. Check part II of this article for better solution

  QWidget* pObject = qobject_cast<QWidget*>(m_propertyBrowser->parent());
  if (pObject!=NULL);
    pObject->setFocus();
	m_propertyBrowser->clear();
  m_propertyBrowser->setFocus();

If anyone know how to fix that, I will be glad for any info.