Application crashes while accessing Pointers and while Hovering over custom QGraphicsItems
-
Hello everyone, I have been working on a project for a while now in which I try to visualize data changes within a graph representing a city network. When I started this project I had never programmed anything with qt, but I had some knowledge about basic c++ (from around 2 semesters at university) and I learned a lot while working on this project. However, I don’t really know what to do right now, which is why I am posting my code and my problem in here (bare with me, I'll try to explain it as best as I can, but I might not use the correct words).
In the application I am building I am using the QGraphicsView framework. Originally I implemented a datastructure for my graph and I wanted to connect it directly to any visualization, but I decided to write a program just for the purpose of visualizing whatever's going to happen in the other program (I'll put them together once I get the visualization part running). So, it might be a little confusing reading through my code since it only serves the purpose of understanding QGraphicsView a little bit better and being able to work purposefully once I put both codes together. Right now, my program only consists of the classes Main, MainWindow, Slice (custom class derived from QGraphicsEllipseItem) and Circle (custom class derived from QGraphicsEllipseItem in my code sometimes called "Kreis" or methods called something like "addKreis" - meaning "addCircle" I might've not caught each one).
Within each object of the class Circle 3 objects of the class Slice are initialized in order to create a piechart (I know, there's a class for that in qt, but I am kind of tring to get it done on my own). Several piecharts are then connected through objects of the class QGraphicsLineItem (as you can see in the screenshot showing an earlier stage some weeks ago (ignore the second non-connected circle on the left though)).
The user is able to move each piechart around in order to get closer to the real distribution of the cities represented by each piechart.
Due to different circumstances I decided to create the class Slice (I used to use Circle for each object, meaning that a circle object always had 3 private variables of other circle objects as children), but since I did that I am stumbling over error after error, most recently these two:ASSERT: "size == 0 || offset < 0 || size_t(offset) >= sizeof(QArrayData)" in file D:/qt/5.8/mingw53_32/include/QtCore/qarraydata.h, line 60
Or
"terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc"
(I've to say that bad_alloc has, however, become more rare – I might've caught that one when checking all member variables and their initialization again)
Concerning the first error, I thought that maybe this error is thrown due to some kind of bad usage of QString objects, but honestly, I am not sure if that is even how it works.
Right now the application does not even start, it just throws the first error and when I try to use the debugger, all I get to see is this:
(but I dont really know what I should actually do with this information and if anyone can explain to me, how this is helpful and what it is supposed to tell me except "segmentation fault" in connection to the Disassembler I'd be more than thankful.)
What I tried to do mostly includes using the Debugger in order to find the spots that cause the errors to be thrown. I looked through each member variable and its initialization again to make sure that no undefined-allocations take place and I replaced most of the pointers I used beforehand with QScopePointers in order to be able to check whether one of the pointers is NULL and of course I tried to figure it all out with the help of qt's documentation (which has been very helpful for a lot of things).
I am not really sure if I explained my problem properly, but I don’t have anyone to talk to in my life when it comes to qt and c++. Also, I would really like to avoid such errors in the futures and restructure my overall coding in order to do so. Basically I've the feeling that I cannot access any of the Slice-pointers without causing a crash and I would like to know why.
Thank you for reading through all of this (and maybe even my code, I'll try to only put in the relevant parts of it).
MainWindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsEllipseItem> #include <QGraphicsLineItem> #include <QGraphicsView> #include "Circle.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void setView(QGraphicsView * v); private slots: //not important right now, only some buttons that dont do anything private: Ui::MainWindow *ui; QGraphicsView * view; QGraphicsScene * scene; };
MainWindow.cpp:
#include "mainwindow.h" #include "ui_mainwindow.h" #include "Circle.h" #include <QtDebug> #include <QGraphicsItem> #include <QGraphicsScene> #include <QApplication> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::setView(QGraphicsView * v){ this->scene = v->scene(); this->view = ui->graphicsView; view->setScene(scene); view->setVisible(true); view = v; ui->graphicsView->setScene(v->scene()); }
Circle.h: (without getter and setter)
#ifndef Circle_H #define Circle_H #include <QGraphicsEllipseItem> #include <QPair> #include <QPaintEvent> #include <QScopedPointer> #include <QString> class Slice; class Circle : public QGraphicsEllipseItem { public: Circle (const QRectF &rect, QString name, Circle * parent = nullptr); void addLine(QGraphicsLineItem *line, bool isPoint1); QVariant itemChange(GraphicsItemChange change, const QVariant &value); void moveLineToCenter(QPointF newPos); void hoverEnterEvent(QGraphicsSceneHoverEvent *); void hoverLeaveEvent (QGraphicsSceneHoverEvent *); void mouseMoveEvent(QGraphicsSceneMouseEvent * event); void checkPointers(); QList <QScopedPointer <Slice> > getSlices(); void updateSlice(QString category, qreal data); int getNumberOfSlices() const; private: QGraphicsLineItem *line; QGraphicsTextItem * text; //a label that specifies the object when the cursor hovers over it //for each link a bool variable (isP1) is stored within the list of links QPair <bool, QGraphicsLineItem *> currPair; QList <QPair<bool, QGraphicsLineItem *>> links; QString name; qreal population; int numberOfSlices; bool isP1; bool hover; QScopedPointer <Slice> s_slice; QScopedPointer <Slice> i_slice; QScopedPointer <Slice> r_slice; }; #endif // Circle_H
Circle.cpp: (without getter/setter)
#include "Circle.h" #include <QBrush> #include <QPainter> #include <QDebug> #include <QGraphicsScene> #include <QRgb> #include <QPainter> #include <QPaintDevice> #include <Slice.h> #include <QScopedPointer> #include <Slice.h> #include <QApplication> Circle::Circle(const QRectF &rect, QString name, Circle * parent) : QGraphicsEllipseItem (rect), name (name), population (100), s_slice (new Slice (this->rect(), int(this->population), int(this->population), QString("Susceptible"), this)), i_slice (new Slice(this->rect(), int(this->population), 0, QString("Infected"), this)), r_slice (new Slice (this->rect(), int(this->population), 0, QString("Removed"), this)) { this->setBrush(QBrush(Qt::darkBlue, Qt::SolidPattern)); setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsScenePositionChanges | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemClipsChildrenToShape); this->setAcceptHoverEvents(true); hover = false; isP1 = false; this->line = new QGraphicsLineItem(); this->text = new QGraphicsTextItem(); this->numberOfSlices = 0; /* each city is basically just a circle with a thick outline and no filling color */ this->setZValue(1); QBrush testBrush (Qt::black); //different colors for different cities (not yet implemented) QPen testPen (Qt::black); testPen.setWidth(10); this->setPen(testPen); this->setBrush(Qt::NoBrush); //at first only s_slice visible == 100% population s_slice->setPos(this->pos()); s_slice->setZValue(0); s_slice->setParentItem(this); this->numberOfSlices ++; //I-Slice i_slice->setPos(this->pos()); i_slice->setZValue(0); i_slice->setParentItem(this); this->numberOfSlices ++; //R-Slice r_slice->setPos(this->pos()); r_slice->setZValue(0); r_slice->setParentItem(this); this->numberOfSlices ++; this->slices.append(s_slice); this->slices.append(i_slice); this->slices.append(r_slice); } void Circle::addLine(QGraphicsLineItem *newLine, bool isPoint1){ currPair.first = isPoint1; currPair.second = newLine; if (this->links.isEmpty()){ this->line = newLine; isP1 = isPoint1; //hochgenommen, damit nicht doppelt eingefügt wird. this->links.append(currPair); }else{ //davor links.append(this->line); links.append(newLine); links.append(currPair); } } QVariant Circle::itemChange(GraphicsItemChange change, const QVariant &value){ if (change == ItemPositionChange && scene()){ //value is the new pos QPointF newPos = value.toPointF(); prepareGeometryChange(); moveLineToCenter(newPos); } return QGraphicsItem::itemChange(change,value); } void Circle::moveLineToCenter(QPointF newPos){ //Converts the ellipse pos (top-left) //to its center-pos int xOffset = int(rect().x() + rect().width()/2); int yOffset = int(rect().y() + rect().height()/2); QPointF newCenterPoint = QPointF(newPos.x()+ xOffset, newPos.y() + yOffset); //Move Line to center for (int i = 0; i < links.size(); i++){ currPair = links.at(i); QPointF p1 = currPair.first ? newCenterPoint : currPair.second->line().p1(); QPointF p2 = currPair.first ? currPair.second->line().p2() : newCenterPoint; links.at(i).second->setLine(QLineF(p1,p2)); } QPointF textPoint = newCenterPoint; textPoint.setX(this->x() + 100); text->setPos(textPoint); } void Circle::mouseMoveEvent(QGraphicsSceneMouseEvent * event){ QGraphicsItem::mouseMoveEvent(event); //CHECK BOUNDS still missing } //probably causes crash void Circle::hoverEnterEvent(QGraphicsSceneHoverEvent *){ text->setPlainText(this->getName()); int xOffset = int(rect().x() + rect().width()/2); int yOffset = int(rect().y() + rect().height()/2); QPointF newCenterPoint = QPointF(this->pos().x()+ xOffset, this->pos().y() + yOffset); QPointF textPoint = newCenterPoint; textPoint.setX(this->x() + 100); text->setPos(textPoint); text->setVisible(true); hover = true; } //probably causes crash void Circle::hoverLeaveEvent(QGraphicsSceneHoverEvent *){ if (this->isSelected()) setVisible(true); else if (text) text->setVisible(false); hover = false; } void Circle::addSlice(QScopedPointer <Slice> s) { this->slices.append(s); //s->setParentItem(this); should no longer be necessary s->setVisible(true); return; } QList <QScopedPointer<Slice>> Circle::getSlices() { QList <QScopedPointer <Slice>> sliceList; sliceList.append(this->s_slice); sliceList.append(this->i_slice); sliceList.append(this->r_slice); return sliceList; } //causes crash void Circle::checkPointers() { if (!s_slice.isNull()) qDebug() << "S-Slice existiert mit den Werten: " << s_slice.data()->getName() << s_slice.data()->getPPopulation(); if (!i_slice.isNull()) qDebug() << "i-Slice existiert : " << i_slice.data()->getName() << i_slice.data()->getPPopulation() ; }
Slice.h (without getter/setter except getName() and getSpanAngle()
#ifndef SLICE_H #define SLICE_H #include <QGraphicsEllipseItem> #include <Circle.h> #include <QString> class Slice : public QGraphicsEllipseItem { public: // parent rect, population part, population category, parent object Slice (QRectF p_rect, int totalPop, int pop_value, QString category, Circle* parent); //updates data & correlating spanAngle void updateData(QString category, qreal angle); QVariant itemChange(GraphicsItemChange change, const QVariant &value); void hoverEnterEvent(QGraphicsSceneHoverEvent *); void hoverLeaveEvent (QGraphicsSceneHoverEvent *); QString getName() const; int getSpanAngle(int value); private: QScopedPointer <QGraphicsTextItem> text; qreal angle; QString category; qreal populationPart; int totalPopulation; int initialPopulationvalue; }; #endif // SLICE_H
Slice.cpp (see above)
#include "Slice.h" #include <QDebug> #include <QBrush> #include <QScopedPointer> #include <QString> Slice::Slice(QRectF p_rect, int totalPop, int pop_value, QString category, Circle * parent) : text (new QGraphicsTextItem(this)), category (category), populationPart (qreal(pop_value)) { this->setRect(p_rect); this->populationPart = pop_value; this->initialPopulationvalue = pop_value; //in order to be able to access initial value this->totalPopulation = totalPop; this->setSpanAngle(getSpanAngle(pop_value)); this->category = category; this->setParentItem(parent); this->setPos(parent->pos()); this->angle = this->spanAngle(); this->setAcceptHoverEvents(true); text.data()->setPlainText(category); text.data()->setVisible(false); // "SUSCEPTIBLE" if (category == "Susceptible") this->setBrush(Qt::green); // "INFECTED" if (category == "Infected") this->setBrush(QBrush(Qt::darkRed)); // "RECOVERED/REMOVED" if (category == "Removed") this->setBrush(Qt::darkGray); qDebug() << "Ein Objekt der Klasse Slice wurde erstellt." << "Daten: " << this->getName() << this->getPPopulation(); } QString Slice::getName() const { return this->category; } int Slice::getSpanAngle(int value) { int population = totalPopulation; if (value < 0) return 0; //ueberdenken! if (value > population) return 5760; return int((5760.0/100.0) * (value/(population/100.0))); } void Slice::updateData(QString category, qreal data){ if (this->category != category) return; else { this->populationPart = int(data); this->angle = getSpanAngle(int(data)); this->setSpanAngle(int(angle)); } //not yet done } QVariant Slice::itemChange(GraphicsItemChange change, const QVariant &value){ if (change == QGraphicsItem::ItemSelectedChange){ prepareGeometryChange(); return QGraphicsItem::itemChange(change,value); } prepareGeometryChange(); return QGraphicsItem::itemChange(change,value); } void Slice::hoverEnterEvent(QGraphicsSceneHoverEvent *) { if (text.isNull()) return; //getPercentage() only returns correlating percentage in relation to total population QString percentageAsString = QString::number(int(this->getPercentage())); text.data()->setPlainText(this->category + " " + percentageAsString + "% "); int xOffset = int(rect().x() + rect().width()/2); int yOffset = int(rect().y() + rect().height()/2); QPointF newCenterPoint = QPointF(this->pos().x()+ xOffset, this->pos().y() + yOffset); QPointF textPoint = newCenterPoint; textPoint.setX(this->x() + 100); text.data()->setPos(textPoint); text.data()->setVisible(true); } void Slice::hoverLeaveEvent(QGraphicsSceneHoverEvent *) { if (text.isNull()) return; text.data()->setVisible(false); }
Main.cpp
#include "mainwindow.h" #include <QApplication> #include <QGraphicsView> #include <QGraphicsScene> #include <QGraphicsItem> #include <QGraphicsEllipseItem> #include <QGraphicsRectItem> #include <QDebug> #include <QLine> #include <QList> #include <Circle.h> #include <Slice.h> QT_CHARTS_USE_NAMESPACE int main(int argc, char *argv[]) { QApplication a (argc, argv); MainWindow w; QGraphicsView view; QGraphicsScene * Q = new QGraphicsScene(0,0,600,600, &view); view.setRenderHint(QPainter::Antialiasing); QString name = "Gruen"; Circle * ellipse1 = new Circle(QRectF(0, 0,100,100), name, nullptr); ellipse1->checkPointers(); ellipse1->movePos(QPointF(200, 200)); Q->addItem(ellipse1); name = "Rot"; Circle *ellipse2 = new Circle(QRectF(0, 0,100, 100), name, nullptr); ellipse2->moveBy(300,300); Q->addItem(ellipse2); QGraphicsLineItem * line = Q->addLine(QLineF(ellipse1->boundingRect().center().toPoint(), ellipse2->boundingRect().center().toPoint()), QPen(Qt::blue, 3)); line->setZValue(-1); ellipse1->setBrush(QBrush(Qt::green)); ellipse2->setBrush(QBrush(Qt::red)); //hinzufuegen der Verbindungen ellipse1->addLine(line, true); ellipse2->addLine(line, false); ellipse1->moveLineToCenter(ellipse1->pos()); ellipse2->moveLineToCenter(ellipse2->pos()); Q->addItem(ellipse1->textItem()); Q->addItem(ellipse2->textItem()); name = "Blau"; Circle * ellipse3 = new Circle(QRectF(0, 0, 100, 100), name, nullptr); ellipse3->moveBy(500,200); line = Q->addLine(QLineF(ellipse1->boundingRect().center().toPoint(), ellipse3->boundingRect().center().toPoint()), QPen(Qt::blue, 3)); //moves line behind slices and circles line->setZValue(-1); ellipse1->addLine(line, true); ellipse3->addLine(line, false); ellipse1->moveLineToCenter(ellipse1->pos()); ellipse3->moveLineToCenter(ellipse3->pos()); line = Q->addLine(QLineF(ellipse2->boundingRect().center().toPoint(), ellipse3->boundingRect().center().toPoint()), QPen(Qt::blue, 3)); line->setZValue(-1); ellipse2->addLine(line, true); ellipse3->addLine(line, false); ellipse2->moveLineToCenter(ellipse2->pos()); ellipse3->moveLineToCenter(ellipse3->pos()); Q->addItem(ellipse3); Q->addItem(ellipse3->textItem()); //only appends a list in MainWindow filled with Circle objects w.addKreis(ellipse1); w.addKreis(ellipse2); w.addKreis(ellipse3); view.setWindowTitle("My network"); view.setScene(Q); w.setView(&view); w.show(); return a.exec(); }
This is the current version and I did not know what is needed in terms of evaluation and actually providing you with enough information about my problem. I guess I'm passing a pointer somewhere (maybe several times) and I am doing that in a bad way. I've the feeling that I'm doing something fundamentally wrong but I dont get what it is and why it happens. Thank you again for reading through all of this. Also I hope that this is the right place in the forum to post it. If it is not, I would kindly ask it to be moved elsewhere and I apologize for the inconvenience.
-
Hi and welcome to the forums
When does the crash happen?
When you call some function or when you close app ? -
@mrjj Hey, if i put the breakpoint where you implied it does not even get to the breakpoint and I only get the segfault (see above in screenshot) again.
Even after commenting // ellipse1->checkPointers(); out it still behaves the same way while debugging.
However when starting the application it starts now without crashing immediately (after taking out / ellipse1->checkPointers();), it only crashes once I hover over the objects.
But I'll try to find the position where it crashes and I'll update this message or post another reply once I know.
-
Hi,
The stack trace you posted seems to be from a release build. Can you share the stack trace from your debug build ?
-
Hi! Thanks again for your replies and your interest in my current problem.
I'm not quite sure if I did this the correct way, but I think I figured out how to get what you asked and I hope that this information is what you've been looking for.This should be the call stack from my debug build (at least I hope it is :'D):
1 ntdll!RtlGetCurrentServiceSessionId 0x77ae4cd9 2 ?? 0xb80afd3f 3 ?? 0x1ef069b0 4 ntdll!RtlFreeHeap 0x77ae3812 5 ntdll!RtlpNtSetValueKey 0x77b82ea1 6 ntdll!RtlGetCurrentServiceSessionId 0x77ae3c45 7 ?? 0x1ef069b8 8 ntdll!RtlFreeHeap 0x77ae3812 9 msvcrt!free 0x76a070b5 10 ?? 0x1f0000 11 QWindowsFontDatabase::releaseHandle qwindowsfontdatabase.cpp 1601 0x62a928b6 12 QtFontStyle::~QtFontStyle qfontdatabase.cpp 234 0x1372329 13 QtFontFoundry::~QtFontFoundry qfontdatabase.cpp 303 0x138530c 14 QtFontFamily::~QtFontFamily qfontdatabase.cpp 365 0x1375138 15 QFontDatabasePrivate::free qfontdatabase.cpp 482 0x13a8915 16 QFontDatabasePrivate::~QFontDatabasePrivate qfontdatabase.cpp 470 0x13a89af 17 (anonymous namespace)::Q_QGS_privateDb::Holder::~Holder qfontdatabase.cpp 720 0x1121bed 18 __tcf_0 qfontdatabase.cpp 720 0x1121c0a 19 _CRT_INIT *12 0x10511b4 20 __DllMainCRTStartup 0x1051325 21 DllMainCRTStartup *12 0x1051433 22 ntdll!RtlCheckTokenCapability 0x77b0a896 23 ntdll!RtlHashUnicodeString 0x77ae01a6 24 ntdll!LdrShutdownProcess 0x77ad458d 25 ??
I hope this helps. Enjoy your weekend and thanks again!
-
@Lanparty said in Application crashes while accessing Pointers and while Hovering over custom QGraphicsItems:
void Slice::hoverEnterEvent(QGraphicsSceneHoverEvent *) { if (text.isNull()) return; QString percentageAsString = QString::number(int(this->getPercentage())); text.data()->setPlainText(this->category + " " + percentageAsString + "% "); int xOffset = int(rect().x() + rect().width()/2); int yOffset = int(rect().y() + rect().height()/2); QPointF newCenterPoint = QPointF(this->pos().x()+ xOffset, this->pos().y() + yOffset); QPointF textPoint = newCenterPoint; textPoint.setX(this->x() + 100); text.data()->setPos(textPoint); text.data()->setVisible(true); }
so, I actually managed to find out where my application crashed. The problem was this line:
text.data()->setPlainText(this->category + " " + percentageAsString + "% ");
once I checked the expected parameters it kind of made sense. I'll try to find another way to achieve the string I originally wanted to be displayed.
-
Do you mean you were missing the
text.isNull()
check ? -
@SGaist No, that's the first thing I did inside the method. However, I simply assumed that I could use the method setPlainText() as I pleased, but it was expecting other arguments and crashed instead (I guess because the QString text would've been longer than the original one.
btw: how do I change the status of my post to "solved"?
-
@Lanparty said in Application crashes while accessing Pointers and while Hovering over custom QGraphicsItems:
btw: how do I change the status of my post to "solved"?
First post, Topic Tools button to the side.