Results 1 to 6 of 6

Thread: Seg fault, but why?

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Seg fault, but why?

    Hi,

    if my code looks like:
    Qt Code:
    1. void binoverview::showNewBinDgl()
    2. {
    3. dgl_newbin dialog;
    4. if ( dialog.exec() == QDialog::Accepted )
    5. {
    6. generatingInformations();
    7. if ( ui.bins->count() > 0 ) // These two lines
    8. ui.bins->setCurrentRow ( 0 ); // will be important
    9. }
    10. }
    11.  
    12. void binoverview::generatingInformations()
    13. {
    14. ui.bins->clear();
    15. QStringList boxes = config::in()->currentUserBins.keys();
    16. for ( int i = 0; i < boxes.size(); ++i )
    17. ui.bins->addItem ( new QListWidgetItem ( QIcon ( ":icon_box_list" ), boxes.at ( i ) ) );
    18. }
    To copy to clipboard, switch view to plain text mode 

    all is well, but if i put the two lines at the end in of generatingInformations(), I get an segmentation fault:
    Qt Code:
    1. void binoverview::showNewBinDgl()
    2. {
    3. dgl_newbin dialog;
    4. if ( dialog.exec() == QDialog::Accepted )
    5. {
    6. config::in()->setCurrentUser ( config::in()->currentUser.value ( "name" ).toString() );
    7. generatingInformations();
    8. qWarning() << "## in showNewBinDgl() if scope"; // this will [B]not[/B] be printed -> seg fault befor
    9. }
    10. }
    11.  
    12.  
    13.  
    14. void binoverview::generatingInformations()
    15. {
    16. ui.bins->clear();
    17. QStringList boxes = config::in()->currentUserBins.keys();
    18. for ( int i = 0; i < boxes.size(); ++i )
    19. ui.bins->addItem ( new QListWidgetItem ( QIcon ( ":icon_box_list" ), boxes.at ( i ) ) );
    20. if ( ui.bins->count() > 0 ) // the two
    21. ui.bins->setCurrentRow ( 0 ); // damn lines
    22. qWarning() << "## in generatingInformations()"; // is printed out
    23. }
    To copy to clipboard, switch view to plain text mode 

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

    Lykurg

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Seg fault, but why?

    What does the debugger say? Could you print the backtrace?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Seg fault, but why?

    The output is
    Qt Code:
    1. Program received signal SIGSEGV, Segmentation fault.
    2. [Switching to Thread -1228274000 (LWP 7536)]
    3. 0x080599a7 in QListWidgetItem::text (this=0x0)
    4. at /usr/include/qt4/QtGui/qlistwidget.h:68
    5. 68 { return data(Qt::DisplayRole).toString(); }
    To copy to clipboard, switch view to plain text mode 

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

    ... and now I see the problem:

    I connected :
    Qt Code:
    1. QObject::connect ( ui.bins, SIGNAL ( currentRowChanged ( int ) ),
    2. this, SLOT ( slot_rowChange ( int ) ) );
    To copy to clipboard, switch view to plain text mode 
    and therefore the slot is called with row = -1 (because of ui.bins->clear(). And this is the real line which cause the problem:
    Qt Code:
    1. ui.bins->item ( row )->text()
    To copy to clipboard, switch view to plain text mode 

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


    Lykurg

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Seg fault, but why?

    Quote Originally Posted by Lykurg View Post
    So I have to check row first, but I thought Qt would do this for me...
    void QListWidget::currentRowChanged ( int currentRow ) [signal]
    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.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Seg fault, but why?

    Quote Originally Posted by jpn View Post
    void QListWidget::currentRowChanged ( int currentRow ) [signal]
    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
    Qt Code:
    1. QListWidget::item ( -1 )->text()
    To copy to clipboard, switch view to plain text mode 
    would deliver an empty QString...

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

    Nice Evening,

    Lykurg

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Seg fault, but why?

    Quote Originally Posted by Lykurg View Post
    Yeah, of course that's fine, but I meant that
    Qt Code:
    1. QListWidget::item ( -1 )->text()
    To copy to clipboard, switch view to plain text mode 
    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.
    J-P Nurmi

Similar Threads

  1. Strange segmentation fault
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2009, 20:50
  2. Segmentation fault in QListViewItem
    By ederbs in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2006, 02:19
  3. Replies: 2
    Last Post: 25th March 2006, 07:54
  4. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 17:30
  5. Replies: 18
    Last Post: 22nd February 2006, 21:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.