PDA

View Full Version : Weird Segfault...



chimby
21st January 2009, 23:39
I am not the best at debugging yet, so I am struggling to figure this one out. I am trying to use a custom class based on QTableView. It compiles ok, but when I try to run the program I get the following segfault:



[Thread debugging using libthread_db enabled]
[New Thread 0xb67e86c0 (LWP 23223)]
[New Thread 0xb5b97b90 (LWP 23226)]
[Thread 0xb5b97b90 (LWP 23226) exited]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb67e86c0 (LWP 23223)]
0xb77477fd in QWidget::~QWidget () from /usr/lib/libQtGui.so.4


I don't really know where to start... here is my super simple subclassed QTableView...



#ifndef APPOINTMENTBOOK_H
#define APPOINTMENTBOOK_H

#include <QtCore>
#include <QTableView>

class AppointmentBook : public QTableView
{
Q_OBJECT

public:
AppointmentBook(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);

};

#endif


and the cpp...



#include <QtGui>
#include <QTableView>
#include "appointmentbook.h"

AppointmentBook::AppointmentBook(QWidget *parent)
: QTableView(parent)
{
//QTimer *timer = new QTimer(this);
//connect(timer, SIGNAL(timeout()), this, SLOT(update()));
//timer->start(1000);

//setWindowTitle(tr("Appointment Book"));
//resize(200, 200);

}

void AppointmentBook::paintEvent(QPaintEvent *)
{

//QColor secondColor(208, 22, 13, 255);

//QTime time = QTime::currentTime();

//QPainter painter(this);
//painter.setRenderHint(QPainter::Antialiasing);
//painter.scale(width(), height() / 86400.0);

//painter.setPen(Qt::NoPen);


//painter.setPen(secondColor);

//int lineY = (time.hour()*60*60) + (time.minute()*60) + time.second();
//painter.drawLine(0, lineY, width(), lineY);

}


As you can see it is all commented out because I can't figure out what's going on.

tmat256
21st January 2009, 23:57
I copied and pasted your code and created a simple test program to verify your segfault



#include <QHBoxLayout>
#include <QWidget>
#include <QApplication>
#include "appointmentbook.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QWidget *window = new QWidget;
AppointmentBook *view = new AppointmentBook();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(view);

window->setLayout(layout);
window->show();

return a.exec();
}


And it runs perfectly fine. How are you trying to use AppointmentBook is your app? I suspect your segfault is somewhere else.

You might try commenting out more and more of your code until it works and then add it all back in until it crashes again. qDebug() lines are also good for trying to find the problem.

chimby
22nd January 2009, 15:12
OK I've narrowed it down a bit. It only segfaults when I set the model. I have subclassed QAbstractTableModel


#ifndef CALENDARDAYMODEL_H
#define CALENDARDAYMODEL_H

#include <QtCore>
#include <QAbstractTableModel>
#include <QList>

class CalendarDayModel : public QAbstractTableModel
{

public:

CalendarDayModel(QObject *parent = 0);

int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent) const;
Qt::ItemFlags flags(){}
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role) const;

private:
QList<QString> vert12Hours;
QList<QString> vertHeader;
QList<QString> horizHeader;

};

#endif // CALENDARDAYMODEL_H

and cpp...


#include "calendardaymodel.h"

CalendarDayModel::CalendarDayModel(QObject *parent)
: QAbstractTableModel(parent)
{
vert12Hours.append(QString("12am"));
vert12Hours.append(QString("1am"));
vert12Hours.append(QString("2am"));
vert12Hours.append(QString("3am"));
vert12Hours.append(QString("4am"));
vert12Hours.append(QString("5am"));
vert12Hours.append(QString("6am"));
vert12Hours.append(QString("7am"));
vert12Hours.append(QString("8am"));
vert12Hours.append(QString("9am"));
vert12Hours.append(QString("10am"));
vert12Hours.append(QString("11am"));
vert12Hours.append(QString("12pm"));
vert12Hours.append(QString("1pm"));
vert12Hours.append(QString("2pm"));
vert12Hours.append(QString("3pm"));
vert12Hours.append(QString("4pm"));
vert12Hours.append(QString("5pm"));
vert12Hours.append(QString("6pm"));
vert12Hours.append(QString("7pm"));
vert12Hours.append(QString("8pm"));
vert12Hours.append(QString("9pm"));
vert12Hours.append(QString("10pm"));
vert12Hours.append(QString("11pm"));
int i;
for(i=0;i<vert12Hours.count();i++)
{
vertHeader.append(vert12Hours.at(i));
vertHeader.append(QString(" :10"));
vertHeader.append(QString(" :20"));
vertHeader.append(QString(" :30"));
vertHeader.append(QString(" :40"));
vertHeader.append(QString(" :50"));
}
}

int CalendarDayModel::rowCount(const QModelIndex &parent) const
{
//return vertHeader.count();
return 1;
}

int CalendarDayModel::columnCount(const QModelIndex &parent) const
{
return 2;
}

QVariant CalendarDayModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
{
return QString("OR");
}
else
{
return vertHeader.at(section);
}
}

QVariant CalendarDayModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() < 0 || index.row() >= 10)
return QVariant();
if (role == Qt::DisplayRole)
return "test";
return QVariant();
}

I try to set the model by...


//Load Default Appointment Book
QAbstractTableModel *model = new CalendarDayModel;
calViewTable->setModel(model);

and that is when it segfaults.

chimby
22nd January 2009, 23:31
Anyone? I am all out of ideas.

thanks.