PDA

View Full Version : Application crashes when associating one object with two others



Caolan O'Domhnaill
12th October 2016, 00:11
Hello all,

I have been modifying the sample application located here: http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html and converted it from a Scene to a View based app. I have added my own QGraphicsPolygonItem called PhysParticle.

The goal of the application is to create particles and apply vectors to them. They should move around the screen and drag the vectors with them.

The following cases work:
Arrow-end of vector connects to a particle
start of vector connects to particle
vector by itself (no particles attached to start or end)

Crashes the application: Both the start and end of vector connects to two separate particles.

I have narrowed it down to a single line of code but it makes absolutely no sense. All it is doing is a member variable assignment. if I take that one item out of the code, everything runs but of course the two particles are not connected.



Arrow *PhysGraphicsView::createVector(QPointF StartPt, QPointF EndPt, PhysParticle *pStartItem, PhysParticle *pEndItem) {
Arrow *pArrow = new Arrow(StartPt, EndPt);
pArrow ->startItem(pStartItem);
if (pStartItem) {
pStartItem ->addArrow(pArrow);
pArrow ->startItem(pStartItem);
}
if (pStartItem != pEndItem && pEndItem) {
pEndItem ->addArrow(pArrow);
pArrow ->endItem(pEndItem); // <---- this causes it to crash
}
pArrow ->setColor(m_LineColor);
pArrow ->setZValue(-1000.0);
m_pScene ->addItem(pArrow);
pArrow ->updatePosition();
return pArrow;
}

// partial class definition for Arrow:
class Arrow : public QGraphicsLineItem, PhysBaseItem {
public:
enum { Type = PhysBaseItem::VectorType };

Arrow(PhysParticle *, PhysParticle *, QGraphicsItem * = NULL);
Arrow(QPointF, QPointF, QGraphicsItem * = NULL);
Arrow(QPointF, QPointF, PhysParticle *, PhysParticle *, QGraphicsItem * = NULL);
int type() const { return Type; }
QRectF boundingRect() const;
void setColor(const QColor &color) { m_Color = color; }
PhysParticle *startItem() const { return m_pStartItem; }
PhysParticle *endItem() const { return m_pEndItem; }
void startItem(PhysParticle *pStartItem) { m_pStartItem = pStartItem; }
void endItem(PhysParticle *pEndItem) { m_pEndItem = pEndItem; }
private:
PhysParticle *m_pStartItem, *m_pEndItem;



The errors I get in QTCreator in the Application output is:
Starting .\QTGraphicsSceneExample\debug\QTGraphicsSceneExam ple.exe...
ASSERT: "!isEmpty()" in file ../../../../Qt/5.5/mingw492_32/include/QtCore/qvector.h, line 204
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
.\QTGraphicsSceneExample\debug\QTGraphicsSceneExam ple.exe exited with code 3

And the dialog box I get is:
12160

anda_skoa
12th October 2016, 09:35
The output indicates that you are hitting an assert in QVector, i.e. trying to access and element in an empty vector.

The assert's fatal log should be where the backtrace ends.
What does the backtrace look like?

Cheers,
_