PDA

View Full Version : Seg fault, but why?



Lykurg
8th March 2007, 19:11
Hi,

if my code looks like:


void binoverview::showNewBinDgl()
{
dgl_newbin dialog;
if ( dialog.exec() == QDialog::Accepted )
{
generatingInformations();
if ( ui.bins->count() > 0 ) // These two lines
ui.bins->setCurrentRow ( 0 ); // will be important
}
}

void binoverview::generatingInformations()
{
ui.bins->clear();
QStringList boxes = config::in()->currentUserBins.keys();
for ( int i = 0; i < boxes.size(); ++i )
ui.bins->addItem ( new QListWidgetItem ( QIcon ( ":icon_box_list" ), boxes.at ( i ) ) );
}


all is well, but if i put the two lines at the end in of generatingInformations(), I get an segmentation fault:

void binoverview::showNewBinDgl()
{
dgl_newbin dialog;
if ( dialog.exec() == QDialog::Accepted )
{
config::in()->setCurrentUser ( config::in()->currentUser.value ( "name" ).toString() );
generatingInformations();
qWarning() << "## in showNewBinDgl() if scope"; // this will not be printed -> seg fault befor
}
}



void binoverview::generatingInformations()
{
ui.bins->clear();
QStringList boxes = config::in()->currentUserBins.keys();
for ( int i = 0; i < boxes.size(); ++i )
ui.bins->addItem ( new QListWidgetItem ( QIcon ( ":icon_box_list" ), boxes.at ( i ) ) );
if ( ui.bins->count() > 0 ) // the two
ui.bins->setCurrentRow ( 0 ); // damn lines
qWarning() << "## in generatingInformations()"; // is printed out
}

:crying: please say why in the second place an seg fault occurs.

Lykurg

wysota
8th March 2007, 19:58
What does the debugger say? Could you print the backtrace?

Lykurg
8th March 2007, 22:24
The output is

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1228274000 (LWP 7536)]
0x080599a7 in QListWidgetItem::text (this=0x0)
at /usr/include/qt4/QtGui/qlistwidget.h:68
68 { return data(Qt::DisplayRole).toString(); }


#0 0x080599a7 in QListWidgetItem::text (this=0x0)
at /usr/include/qt4/QtGui/qlistwidget.h:68
#1 0x0805e0d7 in binoverview::slot_rowChange (this=0x818c7c0, row=-1)
at binoverview.cpp:92
#2 0x0807b6b2 in binoverview::qt_metacall (this=0x818c7c0,
_c=QMetaObject::InvokeMetaMethod, _id=2, _a=0xbf930eec)
at .tmp/moc_binoverview.cpp:72
#3 0xb75bf748 in QMetaObject::activate () from /usr/lib/libQtCore.so.4
#4 0xb75c00ca in QMetaObject::activate () from /usr/lib/libQtCore.so.4
#5 0xb7d44ab3 in QListWidget::currentRowChanged () from /usr/lib/libQtGui.so.4
#6 0xb7d47e21 in QListWidget::supportedDropActions ()
from /usr/lib/libQtGui.so.4
#7 0xb7d49dd0 in QListWidget::qt_metacall () from /usr/lib/libQtGui.so.4
#8 0xb75bf748 in QMetaObject::activate () from /usr/lib/libQtCore.so.4
#9 0xb75c00ca in QMetaObject::activate () from /usr/lib/libQtCore.so.4
#10 0xb7d32d69 in QItemSelectionModel::currentChanged ()
from /usr/lib/libQtGui.so.4
#11 0xb7d38766 in QItemSelectionModel::clear () from /usr/lib/libQtGui.so.4
#12 0xb7d4870d in QListWidget::clear () from /usr/lib/libQtGui.so.4
#13 0x0805e1ff in binoverview::generatingInformations (this=0x818c7c0)
at binoverview.cpp:65
#14 0x0805e59d in binoverview::showNewBinDgl (this=0x818c7c0)
at binoverview.cpp:56
#15 0x0807b697 in binoverview::qt_metacall (this=0x818c7c0,
_c=QMetaObject::InvokeMetaMethod, _id=1, _a=0xbf931b3c)
at .tmp/moc_binoverview.cpp:71
#16 0xb75bf748 in QMetaObject::activate () from /usr/lib/libQtCore.so.4
#17 0xb75bf9cf in QMetaObject::activate () from /usr/lib/libQtCore.so.4
#18 0xb7dc16c1 in QAbstractButton::clicked () from /usr/lib/libQtGui.so.4
#19 0xb7be4c49 in QButtonGroup::id () from /usr/lib/libQtGui.so.4
#20 0xb7be62f9 in QAbstractButton::keyPressEvent () from /usr/lib/libQtGui.so.4
#21 0xb7be6512 in QAbstractButton::mouseReleaseEvent ()
from /usr/lib/libQtGui.so.4
#22 0xb799f86e in QWidget::event () from /usr/lib/libQtGui.so.4
#23 0xb7be5795 in QAbstractButton::event () from /usr/lib/libQtGui.so.4
#24 0xb7c5e8b4 in QPushButton::event () from /usr/lib/libQtGui.so.4
#25 0xb7956590 in QApplicationPrivate::notify_helper ()
from /usr/lib/libQtGui.so.4
#26 0xb7958ff3 in QApplication::notify () from /usr/lib/libQtGui.so.4
#27 0xb79b14a9 in QApplication::x11ProcessEvent () from /usr/lib/libQtGui.so.4
#28 0xb79b0038 in QApplication::x11ProcessEvent () from /usr/lib/libQtGui.so.4
#29 0xb79d4b84 in QX11Info::copyX11Data () from /usr/lib/libQtGui.so.4
#30 0xb7454802 in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#31 0xb74577df in g_main_context_check () from /usr/lib/libglib-2.0.so.0
#32 0xb7457d45 in g_main_context_iteration () from /usr/lib/libglib-2.0.so.0
#33 0xb75d16ae in QEventDispatcherGlib::processEvents ()
from /usr/lib/libQtCore.so.4
#34 0xb79d4935 in QX11Info::copyX11Data () from /usr/lib/libQtGui.so.4
#35 0xb75ad2f1 in QEventLoop::processEvents () from /usr/lib/libQtCore.so.4
#36 0xb75ad3fa in QEventLoop::exec () from /usr/lib/libQtCore.so.4
#37 0xb75af7f8 in QCoreApplication::exec () from /usr/lib/libQtCore.so.4
#38 0xb7956007 in QApplication::exec () from /usr/lib/libQtGui.so.4
#39 0x08050331 in main (argc=Cannot access memory at address 0xffffffff
) at main.cpp:41


... and now I see the problem:

I connected :

QObject::connect ( ui.bins, SIGNAL ( currentRowChanged ( int ) ),
this, SLOT ( slot_rowChange ( int ) ) );
and therefore the slot is called with row = -1 (because of ui.bins->clear();). And this is the real line which cause the problem:

ui.bins->item ( row )->text()

So I have to check row first, but I thought Qt would do this for me...


Lykurg

jpn
8th March 2007, 22:29
So I have to check row first, but I thought Qt would do this for me...
void QListWidget::currentRowChanged ( int currentRow ) [signal] (http://doc.trolltech.com/4.2/qlistwidget.html#currentRowChanged)


This signal is emitted whenever the current item changes. The currentRow is the row of the current item. If there is no current item, the currentRow is -1.

:)

Lykurg
8th March 2007, 22:35
void QListWidget::currentRowChanged ( int currentRow ) [signal] (http://doc.trolltech.com/4.2/qlistwidget.html#currentRowChanged)
This signal is emitted whenever the current item changes. The currentRow is the row of the current item. If there is no current item, the currentRow is -1.

Yeah, of course that's fine, but I meant that
QListWidget::item ( -1 )->text() would deliver an empty QString...

Dosen't matter, the error ist found and I have learned more about gbd.

Nice Evening,

Lykurg

jpn
8th March 2007, 22:49
Yeah, of course that's fine, but I meant that
QListWidget::item ( -1 )->text() would deliver an empty QString...
That would mean that QListWidget::item() would have to somehow return a valid pointer for -1, which wouldn't make any sense.

Returns the item that occupies the given row in the list if one has been set; otherwise returns 0.