Hallo,

if I create an path object (class Path : public QObject, public QGraphicsPathItem) in an other std::thread then his parent item (class Anchor : public QObject, public QGraphicsPixmapItem), it crashes.
The funny thing is, that it doesn't crashes if the QMainWindow that holds the QGraphicScene has no parent. If it's parent is a QTabWidget it crashes.


Anchor.cpp
Qt Code:
  1. void Anchor::addDistanceValue(double distanceValue) {
  2. mutex.lock();
  3. bool stillWorking = isWorking;
  4. mutex.unlock();
  5. if(!stillWorking){
  6. mutex.lock();
  7. isWorking = true;
  8. mutex.unlock();
  9. if (wallCheckerThread.joinable()){
  10. //QTime timeStamp = QTime::currentTime();
  11. wallCheckerThread.join();
  12. //qDebug() << "wait to join anker: " << id << "in " << timeStamp.msecsTo(QTime::currentTime()) << "ms";
  13. }
  14.  
  15.  
  16.  
  17. wallCheckerThread = std::thread([&](double distanceValue){
  18.  
  19. QTime timeStamp = QTime::currentTime();
  20.  
  21. classMutex.lock();
  22. mutex.lock();
  23. Path* item = new Path(distanceValue * 100, this); // <<--- here is the problem, the this
  24. mutex.unlock();
  25. //item->setPos(pos());
  26. item->setVisible(false);
  27. //scene()->addItem(item);
  28. classMutex.unlock();
  29.  
  30.  
  31. checkAdditionalPathLossChanges(distanceValue, item);
  32.  
  33. qDebug() << "Anker " << getId() << ": " << timeStamp.msecsTo(QTime::currentTime()) << "ms";
  34.  
  35. QPen pen;
  36. pen.setColor(0x8888ff);
  37. item->setPen(pen);
  38. item->setFlags(0);
  39. item->setAcceptedMouseButtons(Qt::NoButton);
  40.  
  41. distanceVector.push_back(distanceValue);
  42.  
  43. ellipses.append(item);
  44.  
  45. if (distanceVector.length() > 1){
  46. distanceVector.pop_front();
  47.  
  48. classMutex.lock();
  49. delete ellipses.first();
  50. //scene()->removeItem(ellipses.first());
  51. classMutex.unlock();
  52.  
  53. if (!ellipses.isEmpty())
  54. ellipses.pop_front();
  55. }
  56. item->setVisible(true);
  57. mutex.lock();
  58. isWorking = false;
  59. mutex.unlock();
  60. }, distanceValue);
  61. }else{
  62. qDebug() << "skipped anker " << id;
  63. }
  64. }
To copy to clipboard, switch view to plain text mode 

path.h
Qt Code:
  1. #include <QGraphicsItem>
  2. #include <QObject>
  3. #include <QGraphicsSceneMouseEvent>
  4.  
  5. class Path : public QObject, public QGraphicsPathItem
  6. {
  7. Q_OBJECT
  8. Q_INTERFACES(QGraphicsItem)
  9.  
  10. public:
  11. explicit Path(qreal radius, QGraphicsItem *parent = 0):
  12. QGraphicsPathItem(parent) // << here it crashes (path.h 15)
  13. {
  14. myPath = new QPainterPath();
  15. myPath->addEllipse(QRectF(-radius,-radius,radius * 2, radius * 2));
  16. setPath(*myPath);
  17. setZValue(1);
  18. }
  19.  
  20. enum { Type = UserType + 3 };
  21.  
  22. int type() const { return Type; }
  23. void addPainterPath(QPainterPath path){ *myPath += path; setPath(*myPath);}
  24. QPainterPath* getPainterPath() {return myPath; }
  25.  
  26. protected:
  27. // QGraphicsEllipseItem *item;
  28. void mousePressEvent(QGraphicsSceneMouseEvent *event){
  29. QGraphicsPathItem::mousePressEvent(event);
  30. }
  31.  
  32. void mouseMoveEvent(QGraphicsSceneMouseEvent *event){
  33. QGraphicsPathItem::mouseMoveEvent(event);
  34. }
  35. QPainterPath *myPath;
  36. };
To copy to clipboard, switch view to plain text mode 

working if its used this way:

Qt Code:
  1. #include <QApplication>
  2. #include <QFile>
  3. #include <QObject>
  4. #include <QDebug>
  5. #include "serialwidget.h"
  6. #include "radarwidget.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. QApplication app(argc, argv);
  11. RadarWidget radarWidget;
  12. radarWidget.show();
  13. SerialWidget mainWindow;
  14. mainWindow.show();
  15. QObject::connect(&mainWindow, SIGNAL(newDistanceData(int, double)), &radarWidget, SLOT(addNodeDistance(int, double)));
  16. radarWidget.setSettingsProvider(mainWindow.getSettingsProvider());
  17. return app.exec();
  18.  
  19.  
  20. ////////////////////////
  21.  
  22. just as additional info:
  23. the signal is passed to the right anchor
  24.  
  25.  
  26. void RadarGraphicsView::addNodeDistance(int id, double distance){
  27. if(anchors.length() == 0){
  28. QTimer *timer = new QTimer(this);
  29. connect(timer, SIGNAL(timeout()), this, SLOT(calculateLocation()));
  30. timer->start(1000);
  31. QThread::msleep(20);
  32. }
  33.  
  34. for(int i = 0; i < anchors.length(); i++){
  35. if(anchors.at(i)->getId() == id){
  36. anchors.at(i)->addDistanceValue(distance);
  37. anchors.at(i)->update();
  38. this->scene()->update();
  39. return;
  40. }
  41. }
  42. Anchor::classMutex.lock();
  43. Anchor *newAnchor = new Anchor(id);
  44. this->scene()->addItem(newAnchor);
  45. Anchor::classMutex.unlock();
  46. double x = settings->getSetting(id, "x");
  47. double y = settings->getSetting(id, "y");
  48. newAnchor->setPos(x,y);
  49. newAnchor->addDistanceValue(distance);
  50. anchors.append(newAnchor);
  51. }
  52. ////////////////////////////////////////
  53. }
To copy to clipboard, switch view to plain text mode 


it isnt working this way:

Qt Code:
  1. SerialWidget::SerialWidget(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::SerialWidget)
  4. {
  5. ui->setupUi(this);
  6. Pi = 3.1415;
  7. intValidator = new QIntValidator(0, 3000000, this);
  8. ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);
  9. connect(ui->serialPortInfoListBox, SIGNAL(currentIndexChanged(int)), this, SLOT(showPortInfo(int)));
  10. connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)), this, SLOT(checkCustomBaudRatePolicy(int)));
  11. //connect(ui->actionSaveMeasurement, SIGNAL(triggered())
  12. //connect(ui->localEchoCheckBox, SIGNAL(clicked(bool)), ui->console, SLOT(setLocalEchoEnabled(bool)));
  13. fillPortsParameters();
  14. fillPortsInfo();
  15. updateSettings();
  16. this->showMaximized();
  17. deviceData.clear();
  18.  
  19. replayTool = new ReplayTool();
  20. replayTool->moveToThread(&replayToolThread);
  21. connect(replayTool, SIGNAL(sigDataRecieved(QByteArray)), this, SLOT(readData(QByteArray)));
  22. replayToolThread.start(QThread::LowestPriority);
  23.  
  24. ui->cBDeviceSelector->blockSignals(true);
  25. ui->cBDeviceSelector->insertItem(0,"General Settings");
  26. ui->cBDeviceSelector->blockSignals(false);
  27. ui->cBDeviceSelector->setDuplicatesEnabled(false);
  28. on_friisOkPushButton_clicked();
  29. deviceSettings = new SettingsProvider(&friisDefaultSettings);
  30.  
  31. rssiFilter = new RssiFilter();
  32. isReplayPaused = false;
  33. // if the qmainwindow is a chiild of this object ----------------------|
  34. QObject::connect(this, SIGNAL(newDistanceData(int, double)), ui->sWRadar, SLOT(addNodeDistance(int, double))); // <<--- the radar is a child of the mainwindow
  35. ui->sWRadar->setSettingsProvider(deviceSettings);
  36. }
To copy to clipboard, switch view to plain text mode 

the debugger says:

SIGSEGV
Segmantation fault


stack:
Qt Code:
  1. 0 ?? 0x1d2bf51c
  2. 1 QCoreApplicationPrivate::checkReceiverThread 513 0x6b929693
  3. 2 QApplication::notify 2791 0x91eb4ba
  4. 3 QCoreApplication::notifyInternal 935 0x6b929f96
  5. 4 QCoreApplication::sendEvent 237 0x9536429
  6. 5 QGraphicsScene::setFocus 2969 0x94a835a
  7. 6 QGraphicsScenePrivate::setActivePanelHelper 743 0x94a1c63
  8. 7 QGraphicsScene::setActivePanel 5644 0x94b180c
  9. 8 QGraphicsItem::setActive 3188 0x94813fc
  10. 9 QGraphicsItemPrivate::setParentItemHelper 1222 0x947b90a
  11. 10 QGraphicsItem::setParentItem 1685 0x947ce1b
  12. 11 QGraphicsItem::QGraphicsItem 1399 0x947c412
  13. 14 Path::Path path.h 15 0x43010a
  14. 15 Anchor::__lambda2::operator() anchor.cpp 364 0x40f3cb
  15. 16 std::_Bind_simple<Anchor::addDistanceValue(double)::__lambda2(double)>::_M_invoke<0u>(std::_Index_tuple<0u>) functional 1732 0x41184f
  16. 17 std::_Bind_simple<Anchor::addDistanceValue(double)::__lambda2(double)>::operator()(void) functional 1720 0x41173c
  17. 18 std::thread::_Impl<std::_Bind_simple<Anchor::addDistanceValue(double)::__lambda2(double)> >::_M_run(void) thread 115 0x4116a4
  18. 19 libstdc++-6!execute_native_thread_routine C:\Qt\5.3\mingw482_32\bin\libstdc++-6.dll 0xffe58f
  19. 20 pthread_create_wrapper C:\Qt\Tools\mingw482_32\opt\bin\libwinpthread-1.dll 0x64944e4b
  20. 21 wtoi64 C:\Windows\SysWOW64\msvcrt.dll 0x759c0bc4
  21. 22 msvcrt!_beginthreadex C:\Windows\SysWOW64\msvcrt.dll 0x759c0cec
  22. 23 KERNEL32!BaseThreadInitThunk C:\Windows\SysWOW64\kernel32.dll 0x75e5919f
  23. 24 ntdll!RtlInitializeExceptionChain C:\Windows\SYSTEM32\ntdll.dll 0x77e8a8cb
  24. 25 ntdll!RtlInitializeExceptionChain C:\Windows\SYSTEM32\ntdll.dll 0x77e8a8a1
  25. 26 ??
To copy to clipboard, switch view to plain text mode