Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-61650

QLocalSocket waitForReadyRead in thread crashed

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.6.0
    • Network, Network: Sockets
    • None

    Description

      I use QLocalSocket for inter-process communication.

      One server,listen for connect,when new connection comes,create a thread, in the thread qlocalsocket just waitForReadyRead.

      One client.connect to server above and circularly write data.

      code:

      server: 

       

      TestServer::TestServer(QObject *parent) : QObject(parent),
      ptr_server_(NULL)
      {
      }
      void TestServer::Init()
      {
      if (ptr_server_)
      {
      return;
      }
      ptr_server_ = new QLocalServer(this);
      connect(ptr_server_, &QLocalServer::newConnection, this, [=]() {
      qDebug() << "one client";
      QLocalSocket* ptr_socket = ptr_server_->nextPendingConnection();
      connect(ptr_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(OnError(QLocalSocket::LocalSocketError)));
      SocketThread* ptr_thread = new SocketThread(this);
      ptr_thread->SetSocket(ptr_socket);
      ptr_thread->start();
      ptr_socket_list_.append(ptr_socket);
      });
      }
      void TestServer::OnError(QLocalSocket::LocalSocketError socketError)
      {
      qDebug() << socketError;
      }
      void TestServer::Run()
      {
      Init();
      bool r = ptr_server_->listen("TestServer");
      if (!r)
      {
      qDebug() << "listen failed";
      }
      }
      void TestServer::Stop()
      {
      foreach (QLocalSocket* ptr_socket, ptr_socket_list_) {
      ptr_socket->close();
      delete ptr_socket;
      ptr_socket = NULL;
      }
      ptr_server_->close();
      delete ptr_server_;
      ptr_server_ = NULL;
      }

       

      thread:

       

      void SocketThread::run()
      {
       while(!quit_)
       {
       ptr_socket_->waitForReadyRead(500000);
       if (ptr_socket_->isReadable())
       {
       QByteArray byte_array = ptr_socket_->readAll();
       qDebug() << byte_array.length();
      byte_array.clear();
       }
       }
      }
      

       

      client:

       

      TestClient::TestClient(QObject *parent) : QObject(parent),
       ptr_socket_(NULL),
       ptr_timer_(NULL),
       bytes_size_(1),
       total_bytes_(0)
       {
       ptr_timer_ = new QTimer(this);
       ptr_timer_->setSingleShot(false);
       ptr_timer_->setInterval(500);
      connect(ptr_timer_, &QTimer::timeout, this, &TestClient::WriteBytes);
       }
      void TestClient::Run()
       {
       Init();
      ptr_socket_->connectToServer("TestServer", QIODevice::ReadWrite);
       }
      void TestClient::Stop()
       {
       if (ptr_timer_)
       {
       ptr_timer_->stop();
       }
      if (ptr_socket_)
       {
       ptr_socket_->close();
       delete ptr_socket_;
       ptr_socket_ = NULL;
       }
       }
      void TestClient::Init()
       {
       if (ptr_socket_)
       {
       return;
       }
      ptr_socket_ = new QLocalSocket(this);
       connect(ptr_socket_, &QLocalSocket::connected, this, [=]() {
       emit SignalMsg("connected");
      Test(2000, 1024);
       });
       connect(ptr_socket_, &QLocalSocket::readyRead, this, [=]() {
       QByteArray byteArray = ptr_socket_->readAll();
       int length = byteArray.length();
       total_bytes_ += length;
       emit SignalMsg(QString("read %1 bytes total %2 at ").arg(length).arg(total_bytes_) + QDateTime::currentDateTime().toString("ss : zzz"));
       });
       }
      void TestClient::Test(int msec, int size)
       {
       ptr_timer_->stop();
      bytes_size_ = size;
       ptr_timer_->setInterval(msec);
       ptr_timer_->start();
       }
      void TestClient::StopTest()
       {
       ptr_timer_->stop();
       }
      void TestClient::WriteBytes()
       {
       if (ptr_socket_ && ptr_socket_->isWritable())
       {
       char* buf = new char[bytes_size_];
      qDebug() << ptr_socket_->write(buf, bytes_size_);
       delete buf;
       }
       }
      

       

       

      after just few seconds, server crash, As

       Qt5Cored.dll!QByteArray::resize(int size) 行 1578 C++
      Qt5Cored.dll!QRingBuffer::reserve(__int64 bytes) 行 112 C++
      > Qt5Cored.dll!QWindowsPipeReader::startAsyncRead() 行 239 C++
      Qt5Cored.dll!QWindowsPipeReader::notified(unsigned int numberOfBytesRead, unsigned int errorCode, _OVERLAPPED * notifiedOverlapped) 行 215 C++
      Qt5Cored.dll!QtPrivate::FunctorCall<QtPrivate::IndexesList<0,1,2>,QtPrivate::List<unsigned int,unsigned int,OVERLAPPED >,void,void (_thiscall QWindowsPipeReader::)(unsigned int,unsigned int,_OVERLAPPED *)>::call(void (unsigned int, unsigned int, _OVERLAPPED *) * f, QWindowsPipeReader * o, void * * arg) 行 501 C++
      Qt5Cored.dll!QtPrivate::FunctionPointer<void (__thiscall QWindowsPipeReader::*)(unsigned int,unsigned int,_OVERLAPPED *)>::call<QtPrivate::List<unsigned int,unsigned int,_OVERLAPPED *>,void>(void (unsigned int, unsigned int, _OVERLAPPED *) * f, QWindowsPipeReader * o, void * * arg) 行 520 C++
      Qt5Cored.dll!QtPrivate::QSlotObject<void (_thiscall QWindowsPipeReader::*)(unsigned int,unsigned int,_OVERLAPPED *),QtPrivate::List<unsigned int,unsigned int,_OVERLAPPED *>,void>::impl(int which, QtPrivate::QSlotObjectBase * this, QObject * r, void * * a, bool * ret) 行 143 C++
      Qt5Cored.dll!QtPrivate::QSlotObjectBase::call(QObject * r, void * * a) 行 124 C++
      Qt5Cored.dll!QMetaCallEvent::placeMetaCall(QObject * object) 行 493 C++
      Qt5Cored.dll!QObject::event(QEvent * e) 行 1257 C++
      Qt5Widgetsd.dll!QApplicationPrivate::notify_helper(QObject * receiver, QEvent * e) 行 3714 C++
      Qt5Widgetsd.dll!QApplication::notify(QObject * receiver, QEvent * e) 行 3157 C++
      Qt5Cored.dll!QCoreApplication::notifyInternal2(QObject * receiver, QEvent * event) 行 1015 C++
      Qt5Cored.dll!QCoreApplication::sendEvent(QObject * receiver, QEvent * event) 行 227 C++
      Qt5Cored.dll!QCoreApplicationPrivate::sendPostedEvents(QObject * receiver, int event_type, QThreadData * data) 行 1650 C++
      Qt5Cored.dll!QEventDispatcherWin32::sendPostedEvents() 行 1264 C++
      qwindowsd.dll!QWindowsGuiEventDispatcher::sendPostedEvents() 行 76 C++
      Qt5Cored.dll!qt_internal_proc(HWND__ * hwnd, unsigned int message, unsigned int wp, long lp) 行 431 C++
      [外部代码]
      [下面的框架可能不正确和/或缺失,没有为 user32.dll 加载符号]
      Qt5Cored.dll!QEventDispatcherWin32::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags) 行 829 C++
      qwindowsd.dll!QWindowsGuiEventDispatcher::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags) 行 68 C++
      Qt5Cored.dll!QEventLoop::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags) 行 129 C++
      Qt5Cored.dll!QEventLoop::exec(QFlags<enum QEventLoop::ProcessEventsFlag> flags) 行 204 C++
      Qt5Cored.dll!QCoreApplication::exec() 行 1285 C++
      Qt5Guid.dll!QGuiApplication::exec() 行 1599 C++
      Qt5Widgetsd.dll!QApplication::exec() 行 2974 C++
      DEMO.exe!main(int argc, char * * argv) 行 20 C++
      DEMO.exe!WinMain(HINSTANCE__ * _formal, HINSTANCE_ * __formal, char * __formal, int __formal) 行 113 C++

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            tpochep Timur Pocheptsov
            crowww Wang Brazen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes