PDA

View Full Version : Confusing SIGSEV



mtrpoland
5th September 2007, 10:41
#ifndef PAWNBOX_SAFETY_DECLARATION
#define PAWNBOX_SAFETY_DECLARATION
class QPaintEvent;
class QPixmap;
#include <QWidget>
#include <QRectF>
#include <QColor>
#include <QMap>

namespace MTRUTIL {
const int AMOUNT_OF_PAWNS = 7;
}

namespace MTR {
class PawnBox : public QWidget {
Q_OBJECT
public:
PawnBox(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
private:
void startDrag();
QPoint startPos;
bool insidePawn;
//klasa pionka
class Pawn {
public:
Pawn();
Pawn(QColor &color, QRectF &rec);
~Pawn();
const QColor &color() const { return color_; }
const QRectF &rectF() const { return rectF_; }
private:
QColor color_;
QRectF rectF_;
QPixmap *dragPicture;
};
//
Pawn *draggedPawn;
QMap<int, Pawn> pawnCollection;
};
}
#endif



PawnBox::Pawn::Pawn() {
color_ = QColor(0,0,0);
rectF_ = QRectF(5,5,20,20);
dragPicture = new QPixmap(20,20);
}
PawnBox::Pawn::Pawn(QColor &color, QRectF &rec) {
color_ = color;
rectF_ = rec;
dragPicture = new QPixmap(20,20);
}
PawnBox::Pawn::~Pawn() {
delete dragPicture;
}

SIGSEV

Any hint?

marcel
5th September 2007, 11:17
Well, the code you posted cannot cause a crash.
What about the draggedPawn pointer? Where and how do you use it?

Regards

mtrpoland
5th September 2007, 11:26
Actually when I get rid of "delete dragPicture;" in destructor, everything is fine, apart from the tiny memory leak.

marcel
5th September 2007, 11:37
Do you have any other references to that pointer?
Maybe after the class gets destroyed and the pixmap deleted someone still tries to use your pixmap.

There should be no problem in simply deleting a pointer to a pixmap.

Regards

mtrpoland
5th September 2007, 11:57
I know what is wrong.
I create Pawn objects on stack and then I put them inside QMap<int, Pawn> what evokes copy constructor. Therefore a pointer to a pixmap points still to the QPixmap of an old temporary object that is soon destroyed.

marcel
5th September 2007, 12:07
OK then. That seems to be it.