PDA

View Full Version : Loading text with the first line ending with tabulator causes crash



Zorki
15th June 2011, 18:47
Hello,

i've got a program loading several text files and displaying the content using a QLabel embedded in a QScrollarea.
This works quite fine, except when I load a file thats first line ends with a tab. The Text file is still loaded correctly, but when resizing the Label or changing the Stylesheet of the Scrollarea I receive a segfault.

The Objects are all instatieated properly and I can also view other textfiles before producing the crash.


I have no idea, what the actual problem is, i don't even have an idea, what to search for, so maybe someone can give me a hand what to try next.

d_stranz
15th June 2011, 19:05
To diagnose this problem, I would create a tiny test program that simply puts a QLabel in a QScrollArea, and *manually* set the label's text to a string containing tabs, newlines, whatever. If that works correctly without a crash, then you know the problem must be somewhere else in your code, and the crash is just a side effect of something going wrong earlier in the code that trashes the label's internals.

Zorki
15th June 2011, 21:00
I tried to make a small example containing the critical code, but it is not exact the same, since the program already crashes loading the file.


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
QLabel *label;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->label = new QLabel();
this->setCentralWidget(label);


QString filePath("crash.txt");
QFile f(filePath);
if(f.open(QFile::ReadOnly)) {
QTextStream s(&f);

this->label->setText(s.readAll()); // Crash
} else {
this->label->setText(tr("File ") + filePath + tr(" could not be read."));
}
}

MainWindow::~MainWindow()
{

}


Just calling s.readAll() works fine.
I attached the file that causes the crash. Again everything works, if i delete the tab.

stampede
15th June 2011, 21:37
No crash on my machine ( Windows 7, Qt 4.5.2 ).
Maybe run through debugger and check backtrace.

ChrisW67
15th June 2011, 23:11
No crash here with Qt 4.7.2 on Linux. Using the debugger after a full, clean, debug build is a good start. When (if) it crashes read down the backtrace until you find the line in your code that triggered the crash. Look at the parameters you pass at that point for clues.

Also try just the following program to eliminate file handling and other aspects:


#include <QtGui/QApplication>
#include <QtGui/QLabel>

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

QLabel l("d\t\n");
l.show();
return a.exec();
}

Zorki
16th June 2011, 12:16
The shorter program posted by Chris crashes, too. (I am using Qt version 4.6.3 on Linux).

The backtrace suggests that l.show(); causes the crash, but there are no parameters hinting at the problem.

Here is the backtrace:

0 QTextLine::layout_helper(int) /usr/lib/libQtGui.so.4 0 0xb77f1fae
1 QTextLine::setLineWidth(double) /usr/lib/libQtGui.so.4 0 0xb77f3dbc
2 ?? /usr/lib/libQtGui.so.4 0 0xb76d8c0a
3 ?? /usr/lib/libQtGui.so.4 0 0xb76d99c1
4 QFontMetrics::boundingRect(QRect const&, int, QString const&, int, int*) const /usr/lib/libQtGui.so.4 0 0xb77c3f3b
5 ?? /usr/lib/libQtGui.so.4 0 0xb79c5fd1
6 QLabel::minimumSizeHint() const /usr/lib/libQtGui.so.4 0 0xb79c6416
7 QLabel::sizeHint() const /usr/lib/libQtGui.so.4 0 0xb79c653b
8 QWidgetPrivate::adjustedSize() const /usr/lib/libQtGui.so.4 0 0xb75bddd9
9 QWidget::adjustSize() /usr/lib/libQtGui.so.4 0 0xb75c4c3d
10 QWidget::setVisible(bool) /usr/lib/libQtGui.so.4 0 0xb75cdb80
11 QWidget::show qwidget.h 485 0x08048992
12 main main.cpp 9 0x080488ad

Same in the program above: w.show() leads to the segfault.

It seems that this behavior was already detected as a bug in version 4.6.3 and fixed in 4.7.1
(Check http://bugreports.qt.nokia.com/browse/QTBUG-13546 and http://bugreports.qt.nokia.com/browse/QTBUG-11427)

In fact I should have found this earlier on my own, so thank you for helping me nevertheless.