Skip to content

Commit 01f8daa

Browse files
authored
Fix selection segfault (#136)
* fix segfault when user attempts to move bits When a user attempts to select and move both bits and lines, the program segfaults. This occurs when the bit objects that are referenced by the selection are being deleted. This solution resolves the issue by first removing the bits from the selection before deleting them. * remove extra call to clearBits There is no need for clearBits to be called when the right mouse button is pressed. The only action tied with the right mouse button that requires a call to clearBits is moveLines, which calls clearBits already.
1 parent b1a5c10 commit 01f8daa

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

maskromtool.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ void MaskRomTool::moveLine(RomLineItem* line, QPointF newpoint){
17531753
* are not shown.
17541754
*
17551755
* Group selections are not moved with this function, but instead
1756-
* use moveList().
1756+
* use moveLines().
17571757
*/
17581758
if(bitsVisible){
17591759
removeLine(line,false); //Remove the line's bits, but not the line itself.
@@ -1770,16 +1770,17 @@ void MaskRomTool::moveLine(RomLineItem* line, QPointF newpoint){
17701770
}
17711771

17721772
//Moves a group of items by an offset, used while dragging.
1773-
void MaskRomTool::moveList(QList<QGraphicsItem*> list, QPointF offset){
1773+
void MaskRomTool::moveLines(QList<QGraphicsItem*> &list, QPointF offset){
17741774
/* This function's job is to move a list of items by an offset.
17751775
* It's called when you hit the arrow keys or drag with the right
17761776
* mouse button, and it's vital for it to be fast because there's
17771777
* the potential for redrawing a ton of bits on every frame.
17781778
*
17791779
* This implementation drops all bits that are not actively on
1780-
* moving lines as a necessary performance hack. The missing bits
1781-
* are not on moving lines, and they will return as soon as the mouse
1782-
* button is released.
1780+
* moving lines as a necessary performance hack. This includes
1781+
* dropping bits from the selection. The missing bits are not on
1782+
* moving lines, and they will return as soon as the mouse button
1783+
* is released.
17831784
*
17841785
* As a workaround when movements are too slow, use the TAB key
17851786
* to hide the bits for a while.
@@ -1957,7 +1958,11 @@ void MaskRomTool::clearBits(bool full){
19571958

19581959
//This avoids a deep copy, but cannot be partial.
19591960
for(auto i=bits.constBegin(); i!=bits.constEnd(); i++){
1961+
// The bits must first be removed from the selection
1962+
scene->selection.removeOne(*i);
1963+
// Then the scene
19601964
scene->removeItem(*i);
1965+
// And now they can be deleted
19611966
delete *i;
19621967
bitcount--;
19631968
}

maskromtool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class MaskRomTool : public QMainWindow{
128128
//Moves a line to a new location.
129129
void moveLine(RomLineItem* line, QPointF newpoint);
130130
//Moves a group of items by an offset.
131-
void moveList(QList<QGraphicsItem*> list, QPointF offset);
131+
void moveLines(QList<QGraphicsItem*> &list, QPointF offset);
132132
//Inserts a new line, either row or column.
133133
bool insertLine(RomLineItem* line);
134134
//Is a point visible? Handy when dragging.

romscene.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void RomScene::keyPressEvent(QKeyEvent *event){
4141
case Qt::Key_Left: dpos.setX(-1); break;
4242
case Qt::Key_Right: dpos.setX(1); break;
4343
}
44-
maskRomTool->moveList(selection, dpos);
44+
maskRomTool->moveLines(selection, dpos);
4545
break;
4646
default:
4747
QGraphicsScene::keyPressEvent(event);
@@ -150,7 +150,8 @@ void RomScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent){
150150
//here instead of on release so we can have preview
151151
if(mouseEvent->buttons()==Qt::RightButton){
152152
QPointF dpos = mouseEvent->scenePos() - presspos;
153-
maskRomTool->moveList(selection, dpos);
153+
154+
maskRomTool->moveLines(selection, dpos);
154155

155156
// update because we already moved it
156157
presspos = scenepos;
@@ -194,7 +195,6 @@ void RomScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent){
194195
if(mouseEvent->buttons()==Qt::RightButton){
195196
maskRomTool->dragging=true;
196197
maskRomTool->markUndoPoint();
197-
maskRomTool->clearBits(false);
198198
}
199199
}
200200

0 commit comments

Comments
 (0)