0

我的服务器有问题。

当我想在 ThreadPool 中执行任务时服务器崩溃。

这是使用线程池的函数的一部分:

void CSConnection::onReadable(const AutoPtr<ReadableNotification>& pNf)
{
    try
    {
        int n = _socket.receiveBytes(_pBuffer, BUFFER_SIZE);
        if (n > 0)
        {
         //app.logger().information("Packet s " + nb.format(nBytes) + ", group : " + nb.format(_pBuffer[4]) + ", type : " + nb.format(_pBuffer[6]));
            unsigned short packetID;
            packetID = (_pBuffer[4] * 666) + _pBuffer[6];
            switch(packetID)
            {
                case 666:
                    {
                        QuitHandler* qh = new QuitHandler();
                        qh->_con = this;
                        WorkerThreadPool::getInstance().tp->start(*qh);

执行案例 666 时,服务器崩溃。(只有这种情况使用线程池进行测试)

有 WorkerThreadPool 类:

using Poco::ThreadPool;

class WorkerThreadPool
 {
  private:
        WorkerThreadPool()
        {
            tp = new ThreadPool();
        }
        ~WorkerThreadPool()
        {
            tp->joinAll();
            delete tp;
        }
  public:
        ThreadPool * tp;
        static WorkerThreadPool& getInstance()
        {
          static WorkerThreadPool instance;
          return instance;
        }
 };

有 QuitHandler 类:

using Poco::Runnable;


    class QuitHandler : public Runnable
    {
        public:
            QuitHandler(){}
            CSConnection * _con;
            void run();
            virtual ~QuitHandler();
        protected:
        private:
            char * _packet;
    };

并运行定义:

void QuitHandler::run()
{
    Application &app = Application::instance();
    app.logger().information("quith");
    unsigned short len = 9;
    char * reply = new char[len]();
    ///build packet header
    //set len (it's short)
    reply[0] = len & 0xff;
    reply[1] = (len >> 8) & 0xff;
    reply[4] = 3; //group
    reply[6] = 100; //type
    ///body
    //empty
    _con->sendBlocking(reply, len);
    delete [] reply;
}

记录“app.logger().information("quith");”之前的应用程序崩溃。

谢谢。

4

0 回答 0