Results 1 to 5 of 5

Thread: a segmentation fault

  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default a segmentation fault

    I was beaten by a segmentation fault,
    following is my source code

    Qt Code:
    1. void PieceThread::slotReadReady()
    2. {
    3. while(reply!=NULL&&!pauseFlag&&!reply->atEnd())
    4. {
    5. QMutexLocker locker(&mutex);
    6. file->seek(workingPos);
    7. workingPos+=file->write(reply->read(4096)); //segmentation fault happend here!
    8. emit this->signalProgressIncrease(workingPos-oldWorkingPos);
    9. oldWorkingPos=workingPos;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    I promise that the "file" and the "reply" are legal pointer,

    following is the Call Stack Window

    0 QList<QNetworkReplyImplPrivate::InternalNotificati ons>::contains qlist.h 864 0x680cd407
    1 QNetworkReplyImplPrivate::backendNotify qnetworkreplyimpl.cpp 365 0x68048179
    2 QNetworkReplyImpl::readData qnetworkreplyimpl.cpp 873 0x6804a2f8
    3 QIODevice::read qiodevice.cpp 829 0x6a1ab817
    4 QIODevice::read qiodevice.cpp 964 0x6a1abd3b //here is the "read(4096)"
    5 PieceThread::slotReadReady piecethread.cpp 48 0x4048e3
    6 PieceThread::qt_metacall moc_piecethread.cpp 84 0x408a73
    7 QMetaObject::metacall qmetaobject.cpp 237 0x6a2060b0
    8 QMetaCallEvent:laceMetaCall qobject.cpp 535 0x6a210d55
    9 QObject::event qobject.cpp 1217 0x6a212c60
    10 QApplicationPrivate::notify_helper qapplication.cpp 4462 0x7ebde0
    11 QApplication::notify qapplication.cpp 3862 0x7e9732
    12 QCoreApplication::notifyInternal qcoreapplication.cpp 731 0x6a2015f8
    13 QCoreApplication::sendEvent qcoreapplication.h 215 0x6a268308
    14 QCoreApplicationPrivate::sendPostedEvents qcoreapplication.cpp 1372 0x6a2026a3
    15 qt_internal_proc qeventdispatcher_win.cpp 497 0x6a22499f
    16 USER32!GetDC C:\WINDOWS\system32\user32.dll 0 0x77d18734
    17 ?? 0 0x3301a4
    18 ?? 0 0x401
    19 ?? 0

    the line 0's call stack source code is from "qlist.h"

    Qt Code:
    1. Q_OUTOFLINE_TEMPLATE QBool QList<T>::contains(const T &t) const
    2. {
    3. Node *b = reinterpret_cast<Node *>(p.begin());
    4. Node *i = reinterpret_cast<Node *>(p.end());
    5. while (i-- != b)
    6. if (i->t() == t) //segmentation fault happend here
    7. return QBool(true);
    8. return QBool(false);
    9. }
    To copy to clipboard, switch view to plain text mode 




    thank you for helping me!
    Last edited by yaohao@qtcentre; 15th March 2011 at 13:49. Reason: missing [code] tags

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: a segmentation fault

    I'm pretty sure that something is wrong with slots. Note that PieceThread::slotReadReady() will be called from main thread, since PieceThread belongs to this tread (probably)!
    But don't move PieceThread to himself (this is big mistake).
    This is mainly guessing because we don't see enough details.

    Everything what you what to do in separate thread and should react on signals from other threads (emit signals to other threads) move to own QObject. Then create QThread for it and QObject::moveToThread.
    See this useful article.

  3. The following user says thank you to MarekR22 for this useful post:

    yaohao@qtcentre (16th March 2011)

  4. #3
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a segmentation fault

    thank for your help!

    following is the detail code,
    slotReadReady() will be called from subclass QThread

    Qt Code:
    1. #include"piecethread.h"
    2. #include<QtNetwork>
    3. #include <QtGui>
    4. #include"mission.h"
    5.  
    6. PieceThread::PieceThread(QObject *parent, short thn, quint64 bp, quint64 ep, QUrl u, QFile *f):
    7. QThread(parent),threadNumber(thn),beginPos(bp),endPos(ep),url(u),file(f)
    8. {
    9.  
    10. qnam=NULL;
    11. oldWorkingPos=workingPos=beginPos;
    12. pauseFlag=false;
    13. reply=NULL;
    14. }
    15.  
    16. void PieceThread::run()
    17. {
    18. startTheThread();
    19. }
    20.  
    21. QNetworkRequest PieceThread::setupTheRequest()
    22. {
    23. QNetworkRequest request(url);
    24. QByteArray range=QByteArray("bytes=")+
    25. QByteArray::number(beginPos)+
    26. QByteArray("-")+
    27. QByteArray::number(endPos);
    28. request.setRawHeader("RANGE",range);
    29. return request;
    30. }
    31.  
    32. void PieceThread::startTheThread()
    33. {
    34. QNetworkRequest request=setupTheRequest();
    35. qnam=new QNetworkAccessManager;
    36. reply=qnam->get(request);
    37. connect(reply,SIGNAL(readyRead()),this,SLOT(slotReadReady()));
    38. connect(reply,SIGNAL(finished()),this,SLOT(slotFinished()));
    39.  
    40. this->exec();
    41. }
    42.  
    43. void PieceThread::slotReadReady()
    44. {
    45. while(reply!=NULL&&!pauseFlag&&!reply->atEnd())
    46. {
    47. QMutexLocker locker(&mutex);
    48. file->seek(workingPos);
    49. workingPos+=file->write(reply->read(4096));
    50. emit this->signalProgressIncrease(workingPos-oldWorkingPos);
    51. oldWorkingPos=workingPos;
    52. }
    53. }
    54.  
    55. void PieceThread::slotFinished()
    56. {
    57.  
    58. disconnect(reply,SIGNAL(readyRead()),this,SLOT(slotReadReady()));
    59. disconnect(reply,SIGNAL(finished()),this,SLOT(slotFinished()));
    60. reply->deleteLater();
    61. reply=NULL;
    62. qnam->deleteLater();
    63. qnam=NULL;
    64. if(pauseFlag==false)
    65. {
    66. qDebug()<<"piece "+QString::number(threadNumber)+" download complete";
    67. emit this->signalPieceFinished();
    68. }
    69. else
    70. {
    71. qDebug()<<"piece download abort(pause)";
    72. emit this->signalPieceAbort();
    73. }
    74. }
    75.  
    76. void PieceThread::stopTheThread()
    77. {
    78. pauseFlag=true;
    79. reply->abort();
    80. qDebug()<<"piece download pause";
    81. }
    To copy to clipboard, switch view to plain text mode 

    but the fault happend too.
    Last edited by yaohao@qtcentre; 16th March 2011 at 04:48. Reason: spelling corrections

  5. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: a segmentation fault

    Quote Originally Posted by yaohao@qtcentre View Post
    following is the detail code,
    slotReadReady() will be called from subclass QThread
    Nope, you are wrong. Take a look on QObject::connect, there is fifth argument with default value Qt::AutoConnection.
    So connect will detect that qnam is created in PieceThread and PieceThread was created in main thread and will create queued connection, so this slot will be run in main thread.

    Really move this new functionality to separate QObject instead subclassing QThread. You will gain more flexibility (you can do test without QThread), you will solve problems with this slotReadReady.

  6. The following user says thank you to MarekR22 for this useful post:

    yaohao@qtcentre (17th March 2011)

  7. #5
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a segmentation fault

    I've tried what you say(move this new functionality to separate QObject instead subclassing QThread),and the program works wonderful now!

    that qnam is created in PieceThread and PieceThread was created in main thread and will create queued connection
    it means that the signal "readyRead" is emited in subthread,and the slot(slotReadyRead) is run in main,so it equals Qt::QueuedConnection,right?

    thank for your help!
    you are so professional!

Similar Threads

  1. QWT - Segmentation Fault
    By Wojtek.wk in forum Newbie
    Replies: 0
    Last Post: 17th April 2010, 15:29
  2. Segmentation Fault
    By jmc in forum Qt Tools
    Replies: 4
    Last Post: 24th February 2010, 21:08
  3. segmentation fault
    By dreamer in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2008, 08:48
  4. Segmentation Fault?!
    By r07f1 in forum Newbie
    Replies: 2
    Last Post: 11th April 2008, 16:10
  5. Segmentation Fault
    By merry in forum General Programming
    Replies: 4
    Last Post: 12th March 2007, 05:08

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.