1

I'm using Qt for some gui stuff, and I have inherited QGraphicsScene to implement some of my own methods, and one of the things I did was create a list of QGraphicsItems, specifically a 2D array of an object I made which inhertits the QGraphicsItem.

MatrixButton **buttons;

Then, when I initialize the list in this method from the QGraphicsScene, I get a segmentation fault.

void MatrixScene::initScene()
{
    this->setSceneRect(0, 0, this->width*BUTTON_SIZE, this->height*BUTTON_SIZE);
    this->currentFrameIndex = 0;
    this->color = Qt::red;
    this->buttons = new MatrixButton*[this->height];
    for (int i = 0; i < this->height; i++){
        this->buttons[i] = new MatrixButton[this->width];
    }
    for (int x = 0; x < this->width; x++){
        for (int y = 0; y < this->height; y++){
            this->buttons[x][y].setPos(x*BUTTON_SIZE, y*BUTTON_SIZE); //SEGMENTATION FAULT!!!
            this->addItem(&this->buttons[x][y]);
        }
    }
    this->update();
}

When I debug the application, the debugger tells me that the problem is caused by the following line in QGraphicsItem.h:

inline void QGraphicsItem::setPos(qreal ax, qreal ay)
{ setPos(QPointF(ax, ay)); }

Specifically, the fault occurs when ax = 640 and ay = 0, according to the debugger. I don't understand what would be causing the segmentation fault in this case.

Andrew Lalis
  • 894
  • 3
  • 15
  • 26
  • 1
    2 things: what value has this->width? second: I suggest you use `QVector>` instead of `MatrixButton**`. See here http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap (the same for std::vector can be applied for QVector) – Hayt Oct 14 '16 at 10:31
  • @Hayt this->width is 64 for this specific test, and this->height is 32. I still would like to know out of curiosity what is causing the segmentation fault however. – Andrew Lalis Oct 14 '16 at 10:32
  • What type is this->width? Floating-Point? – Trevir Oct 14 '16 at 10:37
  • are you sure your loop is correct? when you check `x < this->width` X should stop at 63. Or do you have in the "real code" there `<=` ? (I assume `BUTTON_SIZE` is 10) – Hayt Oct 14 '16 at 10:38
  • No, I am sure that there will never be an out of bounds error, as x increments from 0 to 63, and `BUTTON_SIZE` is 20, but that should have no effect on the outcome. – Andrew Lalis Oct 14 '16 at 10:41
  • OK. you have the answer now. Note: if you would have had the `QVector()` and used .size() there instead of width etc. you would not have run into that problem ;) – Hayt Oct 14 '16 at 10:43
  • I think I might switch to QVector anyways. – Andrew Lalis Oct 14 '16 at 10:46

1 Answers1

2

Your problem is you are using x as index for row and y as index for column but the boundary conditions are just opposite

So modify your code to :

for (int x = 0; x < this->height; x++){
        for (int y = 0; y < this->width; y++){
            this->buttons[x][y].setPos(x*BUTTON_SIZE, y*BUTTON_SIZE);        
            this->addItem(&this->buttons[x][y]);
        }
    }
Sumeet
  • 8,086
  • 3
  • 25
  • 45